"UserWarning: Possibly corrupt EXIF data" while classifying images

Question: Below is my code for multi image classification. I am getting an error; I think it is because of loading and other for mismatching in dimension somewhere.
The error message starts where the code ends. Can anybody see the problem?
#importing necessary packages
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from PIL import Image
import tflearn
import tensorflow as tf

import tflearn
#for writing text files

import glob
import os
import random
#reading images from a text file
from tflearn.data_utils import image_preloader
import math

IMAGE_FOLDER = 'C:/Users/kdeepshi/Desktop/PyforE/Face-Detection/Train'
TRAIN_DATA = 'C:/Users/kdeepshi/Desktop/PyforE/Face-Detection/training_data.txt'
TEST_DATA = 'C:/Users/kdeepshi/Desktop/PyforE/Face-Detection/test_data.txt'
VALIDATION_DATA = 'C:/Users/kdeepshi/Desktop/PyforE/Face-Detection/validation_data.txt'
train_proportion=0.7
test_proportion=0.2
validation_proportion=0.1

#read the image directories
filenames_image = os.listdir(IMAGE_FOLDER)
#shuffling the data is important otherwise the model will be fed with a single class data for a long time and
#network will not learn properly
random.shuffle(filenames_image)

#total number of images
total=len(filenames_image)
##  *****training data********
fr = open(TRAIN_DATA, 'w')
train_files=filenames_image[0: int(train_proportion*total)]
for filename in train_files:
    if filename[0:4] == 'Mark':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 0\n')
    elif filename[0:5] == 'lucas':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 1\n')
    elif filename[0:3] == 'Ann':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 2\n')
    elif filename[0:5] == 'Henry':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 3\n')
    elif filename[0:5] == 'Hanna':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 4\n')
    elif filename[0:4] == 'Jack':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 5\n')
    elif filename[0:5] == 'Harry':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 6\n')
    elif filename[0:3] == 'Lui':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 7\n')
    elif filename[0:6] == 'Karlos':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 8\n')
    elif filename[0:4] == 'Guan':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 9\n')

fr.close()
##  *****testing data********
fr = open(TEST_DATA, 'w')
test_files=filenames_image[int(math.ceil(train_proportion*total)):int(math.ceil((train_proportion+test_proportion)*total))]
for filename in test_files:
    if filename[0:4] == 'Mark':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 0\n')
    elif filename[0:5] == 'lucas':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 1\n')
    elif filename[0:3] == 'Ann':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 2\n')
    elif filename[0:5] == 'Henry':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 3\n')
    elif filename[0:5] == 'Hanna':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 4\n')
    elif filename[0:4] == 'Jack':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 5\n')
    elif filename[0:5] == 'Harry':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 6\n')
    elif filename[0:3] == 'Lui':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 7\n')
    elif filename[0:6] == 'Karlos':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 8\n')
    elif filename[0:4] == 'Guan':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 9\n')
fr.close()

##  *****validation data********
fr = open(VALIDATION_DATA, 'w')
valid_files=filenames_image[int(math.ceil((train_proportion+test_proportion)*total)):total]
for filename in valid_files:
    if filename[0:4] == 'Mark':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 0\n')
    elif filename[0:5] == 'lucas':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 1\n')
    elif filename[0:3] == 'Ann':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 2\n')
    elif filename[0:5] == 'Henry':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 3\n')
    elif filename[0:5] == 'Hanna':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 4\n')
    elif filename[0:4] == 'Jack':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 5\n')
    elif filename[0:5] == 'Harry':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 6\n')
    elif filename[0:3] == 'Lui':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 7\n')
    elif filename[0:6] == 'Karlos':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 8\n')
    elif filename[0:4] == 'Guan':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 9\n')
fr.close()

#Importing data
X_train, Y_train = image_preloader(TRAIN_DATA, image_shape=(56,56),mode='file', categorical_labels=True,normalize=True)
X_test, Y_test = image_preloader(TEST_DATA, image_shape=(56,56),mode='file', categorical_labels=True,normalize=True)
X_val, Y_val = image_preloader(VALIDATION_DATA, image_shape=(56,56),mode='file', categorical_labels=True,normalize=True)

print ("Dataset")
print ("Number of training images {}".format(len(X_train)))
print ("Number of testing images {}".format(len(X_test)))
print ("Number of validation images {}".format(len(X_val)))
print ("Shape of an image {}" .format(X_train[1].shape))
print ("Shape of label:{} ,number of classes: {}".format(Y_train[1].shape,len(Y_train[1])))

#Sample Image
plt.imshow(X_train[1])
plt.axis('off')
plt.title('Sample image with label {}'.format(Y_train[1]))
plt.show()
print(type(X_test))

#input image
x=tf.placeholder(tf.float32,shape=[None,56,56,3] , name='input_image')
#input class

y_=tf.placeholder(tf.float32,shape=[None, 10] , name='input_class')

input_layer=x
print("Hiiiiiiii No error till this point")

#convolutional layer 1 --convolution+RELU activation
conv_layer1=tflearn.layers.conv.conv_2d(input_layer, nb_filter=64, filter_size=5, strides=[1,1,1,1],
                                        padding='same', activation='relu', regularizer="L2", name='conv_layer_1')

#2x2 max pooling layer
out_layer1=tflearn.layers.conv.max_pool_2d(conv_layer1, 10)

#second convolutional layer
conv_layer2=tflearn.layers.conv.conv_2d(out_layer1, nb_filter=128, filter_size=5, strides=[1,1,1,1],
                                        padding='same', activation='relu',  regularizer="L2", name='conv_layer_2')
out_layer2=tflearn.layers.conv.max_pool_2d(conv_layer2, 10)
# third convolutional layer
conv_layer3=tflearn.layers.conv.conv_2d(out_layer2, nb_filter=128, filter_size=5, strides=[1,1,1,1],
                                        padding='same', activation='relu',  regularizer="L2", name='conv_layer_2')
out_layer3=tflearn.layers.conv.max_pool_2d(conv_layer3, 10)

#fully connected layer1
fcl= tflearn.layers.core.fully_connected(out_layer3, 4096, activation='relu' , name='FCL-1')
fcl_dropout_1 = tflearn.layers.core.dropout(fcl, 0.8)
#fully connected layer2
fc2= tflearn.layers.core.fully_connected(fcl_dropout_1, 4096, activation='relu' , name='FCL-2')
fcl_dropout_2 = tflearn.layers.core.dropout(fc2, 0.8)
#softmax layer output
y_predicted = tflearn.layers.core.fully_connected(fcl_dropout_2, 10, activation='softmax', name='output')
#loss function
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_predicted+np.exp(-10)), reduction_indices=[1]))
#optimiser -
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
#calculating accuracy of our model
correct_prediction = tf.equal(tf.argmax(y_predicted,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# session parameters
sess = tf.InteractiveSession()
#initialising variables
init = tf.global_variables_initializer()
sess.run(init)
saver = tf.train.Saver()
save_path="C:/Users/kdeepshi/Desktop/PyforE/Face-Detection/mark2.ckpt"

g = tf.get_default_graph()

# every operations in our graph
[op.name for op in g.get_operations()]
epoch=5000

batch_size=20
previous_batch=0
for i in range(epoch):
    #batch wise training

    if previous_batch >= len(X_train) :
            previous_batch=0
    current_batch=previous_batch+batch_size
    x_input=X_train[previous_batch:current_batch]
    x_images=np.array(x_input)
    x_images=np.reshape(x_images,[batch_size,56,56,3])
    y_input=Y_train[previous_batch:current_batch]
    y_label=np.reshape(y_input,[batch_size,10])
    previous_batch=previous_batch+batch_size
    _,loss=sess.run([train_step, cross_entropy], feed_dict={x: x_images,y_: y_label})
    if i%500==0:
        n=50 #number of test samples
        X_test=np.array(X_test)
        x_test_images=np.reshape(X_test[0:n],[n,56,56,3])
        y_test_labels=np.reshape(Y_test[0:n],[n,10])
        Accuracy=sess.run(accuracy,feed_dict={x: x_test_images ,y_: y_test_labels})
        print("Iteration no :{} , Accuracy:{} , Loss : {}" .format(i,Accuracy,loss))
        saver.save(sess, save_path, global_step = i)
    elif i % 100 ==0:
        print("Iteration no :{} Loss : {}" .format(i,loss))
x_input=X_val
x_images=np.reshape(x_input,[len(X_val),56,56,3])
y_input=Y_val
y_label=np.reshape(y_input,[len(Y_val),10])

Accuracy_validation=sess.run(accuracy,feed_dict={x: x_images ,y_: y_label})
Accuracy_validation=round(Accuracy_validation*100,2)
print("Accuracy in the validation dataset: {} %".format(Accuracy_validation))

Test_FOLDER = 'C:/Users/kdeepshi/Desktop/PyforE/Face-Detection/Test'

filenames_image = os.listdir(Test_FOLDER)
total=len(filenames_image)
print(total)

test_files=filenames_image[0: int(total)]
for filename in test_files:

    marty=Image.open(Test_FOLDER+'/'+filename)
    marty_resize=marty.resize((56,56),Image.ANTIALIAS)
    marty_resize=np.array(marty_resize)
    marty_test=marty_resize/np.max(marty_resize).astype(float)
    marty_test=np.reshape(marty_test,[1,56,56,3])
    c=sess.run(y_predicted, feed_dict={x: marty_test})
    d= np.argmax(c)
#test your own images
#test_image=Image.open('/path to file')
#test_image= process_img(test_image)
#predicted_array= sess.run(y_predicted, feed_dict={x: test_image})
    #predicted_class= np.argmax(predicted_array)
    if d==0:

        print("This is Mark\n")
    elif d==1:

        print("This is lucas\n")
    elif d==2:
        print("This is Ann")
    elif d==3:

        print("This is Henrry\n")
    elif d==4:

        print("This is Hanna\n")
    elif d==5:
        print("This is Jack")

    elif d==6:

        print("This is Harry\n")
    elif d==7:
        print("This is Lui")
    elif d==8:

        print("This is Karlos\n")
    elif d==9:

        print("This is guan\n")

Here is the error:

C:\Users\kdeepshi\Desktop\PyforE\Face-Detection>multi.py
curses is not supported on this machine (please install/reinstall
  curses for an optimal experience)
Dataset
Number of training images 22
Number of testing images 6
Number of validation images 3
C:\ProgramData\Anaconda3\lib\site-packages\PIL\TiffImagePlugin.py:692:
  UserWarning: Possibly corrupt EXIF data.  Expecting to read 12 bytes
  but only got 0. Skipping tag 270
"Skipping tag %s" % (size, len(data), tag))
C:\ProgramData\Anaconda3\lib\site-packages\PIL\TiffImagePlugin.py:692:
  UserWarning: Possibly corrupt EXIF data.  Expecting to read 6 bytes
  but only got 0. Skipping tag 271
"Skipping tag %s" % (size, len(data), tag))
C:\ProgramData\Anaconda3\lib\site-packages\PIL\TiffImagePlugin.py:692:
  UserWarning: Possibly corrupt EXIF data.  Expecting to read 8 bytes
  but only got 0. Skipping tag 272   "Skipping tag %s" % (size,
  len(data), tag))
C:\ProgramData\Anaconda3\lib\site-packages\PIL\TiffImagePlugin.py:692:
  UserWarning: Possibly corrupt EXIF data.  Expecting to read 64 bytes
  but only got 0. Skipping tag 282   "Skipping tag %s" % (size,
  len(data), tag))
C:\ProgramData\Anaconda3\lib\site-packages\PIL\TiffImagePlugin.py:692:
  UserWarning: Possibly corrupt EXIF data.  Expecting to read 64 bytes
  but only got 0. Skipping tag 283   "Skipping tag %s" % (size,
  len(data), tag))
C:\ProgramData\Anaconda3\lib\site-packages\PIL\TiffImagePlugin.py:692:
  UserWarning: Possibly corrupt EXIF data.  Expecting to read 20 bytes
  but only got 0. Skipping tag 306   "Skipping tag %s" % (size,
  len(data), tag))
C:\ProgramData\Anaconda3\lib\site-packages\PIL\TiffImagePlugin.py:692:
  UserWarning: Possibly corrupt EXIF data.  Expecting to read 24 bytes
  but only got 0. Skipping tag 529   "Skipping tag %s" % (size,
  len(data), tag))
C:\ProgramData\Anaconda3\lib\site-packages\PIL\TiffImagePlugin.py:692:
  UserWarning: Possibly corrupt EXIF data.  Expecting to read 24 bytes
  but only got 0. Skipping tag 532   "Skipping tag %s" % (size,
  len(data), tag))
C:\ProgramData\Anaconda3\lib\site-packages\PIL\TiffImagePlugin.py:692:
  UserWarning: Possibly corrupt EXIF data.  Expecting to read 40 bytes
  but only got 0. Skipping tag 33432   "Skipping tag %s" % (size,
  len(data), tag))
C:\ProgramData\Anaconda3\lib\site-packages\PIL\TiffImagePlugin.py:709:
  UserWarning: Corrupt EXIF data.  Expecting to read 2 bytes but only
  got 0.   warnings.warn(str(msg))
Shape of an image (56, 56, 3)
Shape of label:(10,) ,number of classes: 10
Answer: There is problem reading EXIF data from images in PIL module. I tend to think this is bug in PIL instead of images being corrupt. As this data is not needed for deep learning you can just cleans your files. To do that, download ExifTool and run following command:
exiftool -r -all= -ext JPEG D:\datasets\ImageNet\train

-r: recursive
-all: Set all EXIF data to null
-ext: File extensions
This works on WIndows as well as Linux.
Comment: Thanks a lot. I tried this tool but I got this error `Error: Bad IFD or truncated file in IFD2`. How can this be fixed?


Is there any difference between hypothesis testing $A\beta = a$ and $\beta = A^{-1}a$?

Question: Let $\beta,a_0 \in \Bbb{R}^d$. My professor said we wanted to test
$$H_0: A\beta = a$$
$$H_1: A\beta \ne a$$
However, I don't understand why she would write it like this and I haven't seen hypothesis tests before where the parameter of interest is multiplied by a matrix. Assuming $A$ is invertible, then we could just test
$$H_0: \beta = A^{-1} a$$
$$H_0: \beta \ne A^{-1} a$$
So I can only see an advantage of this if $A$ was not invertible. Are there situations where that is useful or is this just a bad way to notate hypothesis testing?
EDIT:
Here's the context
Comment: Actually, you *have* seen this formulation before: the default parameter tests $\beta_i=0$ (automatically performed by all regression software) are in this form with $A=(0,0,\ldots,0,1,0,\ldots,0).$
Comment: It is unusual indeed but maybe it is taken out of context here? Maybe she wanted to draw attention to something else?
Comment: I have to say, that context looks very clear, and I'm not sure why it would trouble you.
Answer: If you wish to perform a constrained linear regression with linear equality constraints of the form $A\beta = a$, then tests such as the likelihood ratio test are more naturally formulated in this way.  This is because the constrained OLS estimator $\beta^*$ can be written in terms of the unconstrained estimator $\hat{\beta}$, $A$, and $a$:
$$\beta^* = \hat{\beta} + (X'X)^{-1}A'(A(X'X)^{-1}A')^{-1}(a-A\hat{\beta})$$
$\beta^*$ is best linear unbiased, given the constraints.  Our test is then easily seen to be that $a-A\beta=0$, or, slightly rearranged, that $A\beta = a$, whereas the use of the inverse does not jump so readily to the eye.
Also note that it may be that $A$ is not square, and therefore not invertible, for example, if you are constraining $\hat{\beta}_1 + \hat{\beta}_2 = 0$ in your regression and want to test the corresponding hypothesis about $\beta_1$ and $\beta_2$, $A$ would be $1 \times k$, where $k$ is the length of $\beta$.  This may not apply to your case, judging from the first line of the question, but in the more general case it is definitely an issue.
Comment: I don't know what you do and do not have access to, so I don't know whether I have such a reference; mine is Theil, Principles of Econometrics, but I also see it's in Amemiya, Advanced Econometrics.  I'd guess almost any graduate-level econometrics text will have it.
Comment: Do you have a reference for where I could see the derivation of that formula for $\beta^*$? I've not seen in before and knowing that would help me get more from this answer
Answer: It is often the case that $A$ is not a square matrix, and so not invertible.  However, assuming that $A$ is actually invertible, the statement $A \beta = a$ is equivalent to $\beta = A^{-1} a$, and so your formulation of the test is legitimate (though unusual).  Regardless of which form you use to state the hypotheses, the actual content of the test is determined by finding an appropriate test statistic and using it to compute the p-value.  This exercise is not affected by which of the two equivalent forms you use to state the hypotheses.


How can I add the servicenetworking.services.addPeering privilege to an user?

Question: I'm trying to deploy a Cloud SQL instance using private IPs, which have recently become available. It seems I need the servicenetworking.services.addPeering privilege, but I can't find it neither in the roles nor in the APIs.
Is it hidden somewhere else? How can I add this privilege for an user?
Thanks in advance.
Answer: The permission "servicenetworking.services.addPeering" is included in the role "compute.NetworkAdmin", even if it the doc doesn't list it.
If you want to add it to a custom role it isn't working properly yet, even when you can add via CLI, as it isn't specified yet which permissions beside that one you need to be able to create the instance.
Comment: Actual role is "compute.networkAdmin". Sorry to be pedantic, but cli is case sensite and edit has to be at least for 6 chars.
Answer: Two things you need to enable for setting up a cloudsql :
- Enable Google api=  Service Networking API (This is for the project)
- You/email ID logged in creating CloudSQL instacne should have Computer Network Admin.

That it.
CLI:
Enalbe API for your project:
gcloud services enable servicenetworking.googleapis.com --project=your-project-name-here

Elevate Your privilege to Network Admin:
> -Login to google console : console.cloud.google.com
> -Go to IAM
> -Search your logged in email ID and add a Role: "Network Admin"

thats it.
Atfer that you will be able to enable private IP  for cloudSQL.


Manipulating dot products

Question: I know that vectors $a$ and $b$, $a \cdot b = b^Ta$.
So, for a $n \times  n$ matrix $A$, and vectors $x$ and $y$
$$ (Ax) \cdot (Ay) = (Ay)^T (Ax) =y^T (A^T Ax) = (A^T Ax) \cdot  y^T = (A^T A) (x \cdot  y^T)$$
Is this correct? In particular, I know $(ax) \cdot y = a (x \cdot y)$ for $a \in \mathbb{R} $, does this work for matrices?
Comment: Your last equality looks dubious. The last quantity seems to be an $n\times n$ matrix times a scalar, so presumably is an $n\times n$ matrix, while all preceding terms seem to be scalars.
Answer: $$
\newcommand \trans {^\mathsf T}
v \cdot (Mw) = w\trans M\trans v = w\trans (M\trans v) = (M\trans v) \cdot w,
$$
now plug them in.

Manipulating dot products


The Riemannian Distance function does not change if we use $C^1$ paths?

Question: In an answer to this question, it is proved that the Riemann distance function is the same whether we use smooth paths or piecewise smooth paths.
What happens if we use $C^1$ paths? Can we approximate well enough any $C^1$ path with a smooth one?
This question is local: The image of any $C^1$ - path is compact, hence it can be covered by a finite number of coordinate balls; If we can approximate each segment which lies in a coordinate ball, we can approximate any path.
So, the question is reduced to the case of arbitrary metric on $\mathbb{R}^n$.
Attempted solution:
In this paper, it is proved that the Riemannian distance does not change if we use arbitrary absolutely continuous (a.c) paths. (See Corollary 3.13).
Thus, since "smooth $\subseteq C^1 \subseteq$ a.c" we get that
$$ d^{a.c}=d^{C^{\infty}} \ge d^{C^1} \ge d^{a.c},$$ so in particular $d^{C^1}=d^{C^{\infty}}$.
Perhaps there is an easier solution.
Answer: Yes, you can approximate any continuous map by $C^\infty$ maps. The cleanest way to do so is to embed $M$ in some $R^N$ and then use the fact that a small neighborhood $U$ of the image is diffeomorphic to the normal tangent bundle of $M$. In particular, you have a smooth projection $U\to M$. Now, given a continuous map $f: I\to M$, approximate $f$ by smooth functions $f_\epsilon: I\to R^N$ and then compose these approximates with $\pi$. The construction of $f_\epsilon$ is usually done via convolution with suitable bump-functions.  See https://en.wikipedia.org/wiki/Mollifier.
Comment: Thanks for your answer. Actually by the argument I gave it's enough to consider $\mathbb{R}^n$, with arbitrary metric on it. However, if we take your approach, I am not sure I understand why after projecting the $f_\epsilon$ back to $M$ the "goodness" of the approximation is not damaged to much.


java program reports wrong timezone on linux(shows IST for Europe/Dublin)

Question: I get a wrong Time zone value (IST) from the following code. Its from bugreport
import java.util.*;
import java.text.*;
class simpleTest
{
        public static void main(String args[])
        {
           System.out.println("Simple test Josh ");

           Date now = new Date();
           DateFormat df = DateFormat.getDateInstance();
                Calendar cal = Calendar.getInstance();
        System.out.println("\n TIME ZONE :"+ cal.getTimeZone().getDisplayName());
           long nowLong = now.getTime();
           String s = now.toString();
           System.out.println("Value of milliseconds  since Epoch is " + nowLong);
           System.out.println("Value of s  in readable format is " + s);

        }
}

With Dublin, the zone is wrong. It shows IST
$ java -Duser.timezone=Europe/Dublin simpleTest
Simple test Josh

 TIME ZONE :Greenwich Mean Time
Value of milliseconds  since Epoch is 1408095007238
Value of s  in readable format is Fri Aug 15 10:30:07 IST 2014

This one is okay
$ java -Duser.timezone=Europe/Helsinki  simpleTest
Simple test Josh

 TIME ZONE :Eastern European Time
Value of milliseconds  since Epoch is 1408095025866
Value of s  in readable format is Fri Aug 15 12:30:25 EEST 2014

Where does the value IST  come from?
I have checked os files like /etc/localtime
bash-3.2# cd /etc
bash-3.2# ls -lrt localtime
lrwxrwxrwx 1 root root 33 Nov 16  2010 localtime -> /usr/share/zoneinfo/Europe/Dublin

/etc/sysconfig/clock
bash-3.2# cd /etc/sysconfig/
bash-3.2# cat clock
# The ZONE parameter is only evaluated by system-config-date.
# The timezone of the system is defined by the contents of /etc/localtime.
ZONE="Europe/Dublin"
UTC=true
ARC=false
bash-3.2# pwd

Comment: Where's @JonSkeet and his amazing timezone knowledge when you need him.
Comment: Related: http://stackoverflow.com/questions/6392/java-time-zone-is-messed-up
Answer: Irish Standard Time, which is the correct timezone for Dublin, is abbreviated as IST.
Unfortunately timezone abbreviations are not unique, as you can see in this table - IST can be resolved into (at least) two other timezones. Timezone abbreviations are not part of the related ISO standard and their use is not generally recommended.
In general, UTC timestamps should be preferred when multiple timezones are involved. With UTC each party only needs to keep track of at most one timezone apart from their own. In addition, UTC is not affected by DST practices, which are normally a source of confusion even for locations with the same longitude.
Comment: Huh!.  I thought that is wrong as India Standard Time=IST
Comment: Unless there is some context to eliminate some alternatives, timezone abbreviations are useless. Quite honestly, I prefer UTC times...
Comment: Just saw that table. Never thought that way. We are including the timeoffset which is actually more useful.
Comment: I agree. Please add that as recommendation iinto your answer. We decided to use UTC in all logging.  This was for a user visible message (email report)


Is Collection a subtype of Object in Java?

Question: Is Collection<?> a subtype of Object in Java? This might be a stupid question, but isn't Object the root of every class?
Comment: @cdhowie Neither are interfaces. ;)
Comment: Object is the root of every class, except interfaces (like Collection), Object class itself and the primitive classes.
Comment: @Peter: The primitive types are not technically classes. ;)
Answer: No.  Collection is an interface, and interfaces cannot inherit from classes -- therefore they cannot inherit from Object.  (It does not make sense for an interface, which has no implementation, to inherit from a class, which can have an implementation.)
However, any class that implements Collection will obviously have to inherit from Object.  So you can treat objects implementing Collection as though they do inherit from Object, because ultimately they will have to.
It's a semantic difference, but an important distinction in OO theory.
UPDATE: For those challenging this answer with the Java spec, I refer you to this code sample, which indicates that either the spec is not implemented correctly, or is just plain wrong.
TestInterface.java
public interface TestInterface {
    public void foo();
}

Test.java
import java.lang.*;
import java.lang.reflect.*;

public class Test {
    public static void main(String[] args) {
        Class ti = TestInterface.class;

        Class sti = ti.getSuperclass();
        System.out.println("superclass is null: " + (sti == null));

        for (Method m : ti.getMethods()) {
            System.out.println(m.getName());
        }
    }
}

On my system (Java(TM) SE Runtime Environment (build 1.6.0_20-b02) according to java -showversion) the output of the compiled program is:
superclass is null: true
foo
So I'm sorry, but no, interfaces do not technically inherit from anything (except possibly other interfaces) and they do not inherit the methods of Object.
Comment: It's not. I am putting together a code sample that demonstrates this.
Comment: @cdhowie: Also take a look at Tom's updated answer... as he says, section 4.10.2 of the JLS states _specifically_ that any interface type is a subtype of `Object`. That is the correct answer, period. The fact that this got so many upvotes is embarassing.
Comment: The implementation of `Class.getSuperclass` appears to follow its API docs. Like the method name, the API docs do not mention subtype or supertype.
Comment: @Tom Hawtin, @ColinD I agree with you that the documentation says that you are right. And that I didn't even suspect it would be like that I belived that interfaces did not extend from Object, but I have one question, How is that cdhowie code gets a null in its getSuperclass() method? That doesn't defy the very idea behind the documentation? It seems that you can override Object methods even if it seems that de code says it is not a superclass. Could it be "yes and no" a really truthful answer? Thank you :)
Comment: @Random: yes but of why oh why don't you also upvote the question? It's only the question that makes this answer worthy. SO makes me wants to p*ke when I see nonsense like this: a good question that gets only one vote and 9 upvotes for the answer.  I like the answer, but -1 sorry: it's not normal.  and +1 to the question.
Comment: Ah, nothing like being penalized for a good answer because of someone's grudge. \o/
Comment: @cdhowie It's wrong. `Collection` is a subtype of `Object`.
Comment: @cdhowie: Sorry, but you're just plain wrong. An interface _is_ a subtype of `Object`. The fact that you can override methods declared by `Object` (such as `equals` and `hashCode`) on an interface (complete with `@Override` annotation) should be an indication to you here.
Comment: @ColinD: The implementation doesn't follow the spec. See my updated answer.
Comment: @cdhowie: What the reflection API does here is meaningless in the context of this question. The question asks if the interface type `Collection` is a subtype of `Object`, and the only answer to that is _yes_.
Comment: @Webinator: I am terribly sorry that we do not have the same way to judge the upvoting system. At the end it's an opinion system, and your arguments are rather weak in the position that for a terrible question it can exists an incredibly well placed answer. Do you really think that asking if Collection extends Object (with out more data) is a worth-upvoting question? Yes? well go and upvote it. Trolling about it doesn't seem the best way to archive anything.
Comment: @ColinD: Do not confuse inheritance with convertibility.
Comment: @cdhowie: You're the one who's confusing things here, sorry. An interface type _does_ inherit from `Object`. Several facts have been presented to you that make this clear, including: the JLS, the fact that a `Collection>` is valid (when the collection's type parameter must be a subclass of `Object`), and the fact that `@Override` is legal on `Object` methods in interfaces, but you choose to ignore them.
Comment: Also note that the JLS defines inheritance to include inheritance of members from interfaces.
Comment: @Random: No, it's simply that the `getSuperclass()` method does not define the subtype/supertype relationship. As Tom mentions, the method is not `getSupertype()` and doesn't mention "supertype" anywhere. The method also states "If this `Class` represents either the `Object` class, an interface, a primitive type, or void, then null is returned." That is simply the contract of that particular API method, and doesn't say anything about the Java language's definition of what is a subtype of what. In Java, an interface type is a subtype of `Object`.
Answer: Yes, Collection<?> is a reference type and therefore a subtype of java.lang.Object.
This is easy to check:
import java.util.Collection;

class Assign {
    public Object check(Collection<?> things) {
        return things;
    }
}

Just as well, because it allows us to have collections of collections, for instance.
JLS 4.10.2 sayeth:

Given a type declaration for
  C, the direct supertypes of
  the parameterized type (§4.5)
  C are all of the following:
[...]

The type Object, if C is an
  interface type with no direct
  superinterfaces.
Comment: It is not a subtype of `Object`, but it is convertible to `Object`.
Comment: @cdhowie JLS definition of Widening Reference Conversions: "A widening reference conversion exists from any type S to any type T, provided S is a subtype (§4.10) of T." http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#190646
Answer: Yes. Any interface type is a subtype of Object.
I think a lot of the confusion here (by which I mean cdhowie's incorrect answer and the large number of upvotes it has received) centers around an incorrect understanding of the term "subtype". "Subtype" is a compile-time concept defined by the Java Language Specification that indicates a relationship between two types. While the subtype relationship is used various places in the JLS, a type S being a subtype of a type T effectively means that an instance of S is always also an instance of T. Given that, for a reference of type S to be assigned to a reference of type T without casting and without producing a compiler error, S must be a subtype of T!
Thus, the fact that the following code is legal is actually proof that the interface type Collection is considered a subtype of the type Object by the Java langauge, just as the JLS specifies.
Collection<?> collection = ...
Object obj = collection;

cdhowie's "proof" that his answer is correct does not actually prove anything other than that the methods Class.getSuperclass() and Class.getMethods() obey the contracts they specify in their documentation... this has nothing to do with Java's concept of a "type".
Class.getSuperclass():

If this Class represents either the Object class, an interface, a primitive type, or void, then null is returned.

Class.getMethods():

This method returns an array of length 0 if this Class object represents a class or interface that has no public member methods, or if this Class object represents a primitive type or void.
Comment: Well said, especially the contract of "getSuperClass"


How to cast program argument *char as int correctly?

Question: I'm on a Mac OS X 10.6.5 using XCode 3.2.1 64-bit to build a C command line tool with a build configuration of 10.6 | Debug | x86_64. I want to pass a positive even integer as an argument, so I'm casting index 1 of argv as an int. This seems to work, except it seems that my program is getting the ascii value instead of reading the whole char array and converting to an int value. When I enter 'progname 10', it tells me that I've entered 49, which is what I also get when I enter 'progname 1'. How can I get C to read the entire char array as an int value? Google only showed me (int)*charPointer, but clearly that doesn't work.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main(int argc, char **argv) {
    char *programName = getProgramName(argv[0]); // gets the name of itself as the executable
    if (argc < 2) {
        showUsage(programName);
        return 0;
    }
    int theNumber = (int)*argv[1];
    printf("Entered [%d]", theNumber);            // entering 10 or 1 outputs [49], entering 9 outputs [57]
    if (theNumber % 2 != 0 || theNumber < 1) {
        showUsage(programName);
        return 0;
    }

    ...

}

Answer: The numer in the argv array is in a string representation. You need to convert it to an integer.
sscanf
int num;
sscanf (argv[1],"%d",&num);

or atoi() (if available)
Answer: Casting can't do that, you need to actually parse the textual representation and convert into integer.
The classical function for this is called atoi(), but there's also strtol() or even sscanf().
Comment: Don't ever use `atoi()`, it might not be available and it does **no** error checking whatsoever: http://stackoverflow.com/questions/2729460/usage-of-atoi-in-the-c-language
Comment: @Ivo, atoi is specified by the C standard so likely to be everywhere. It does do error checking, it just reports it in a nonhelpful way (as zero). strtol is a better function, except for quick throwaway code, where it is harder to use.
Answer: Lets say you have a 9 as argument. You said you are getting  57, the ascii value.
Use the fact that int num = *arg[1] - '0' will give you back the number you need.
if it's a larger number like, 572 then you need
char *p = argv[1];
int nr = 0;
int i;
for(i =0; i< strlen(p); i++) {
    nr = 10 * nr + p[i] - '0';
}

Add some error checking and its good to go.
Or, depending on what you have, you can use some functions like atoi(), sscanf() etc.
Answer: Use atoi() or strol() functions.


remove columns which contain whole numbers in r

Question: I have the following data:
# A tibble: 49 x 9
   date           Y    X1    X2    X3    X4     X5       X6 ID
   <date>     <dbl> <dbl> <dbl> <dbl> <dbl>  <dbl>    <dbl> <chr>
 1 2016-10-21     1  4.14    18     0     1 0.0770 0.000429 CAT1
 2 2016-10-24     1  4.14    17     0     1 0.0793 0.000424 CAT1
 3 2016-10-25     0  4.16    16     0     1 0.0804 0.000404 CAT1
 4 2016-10-26     0  4.16    15     0     1 0.0626 0.000426 CAT1
 5 2016-10-27     0  4.16    14     0     1 0.0587 0.000442 CAT1
 6 2016-10-28     0  4.11    13     0     1 0.0610 0.000440 CAT1
 7 2016-10-31     0  4.14    12     0     1 0.0704 0.000425 CAT1
 8 2016-11-01     1  3.98    11     0     1 0.0867 0.000417 CAT1
 9 2016-11-02     0  6.00    10     0     1 0.0673 0.000453 CAT1
10 2016-11-03     0  8.42    10     0     1 0.0877 0.000429 CAT1
# ... with 39 more rows

I would like to remove all the columns which contain whole numbers - except Y -.
# A tibble: 49 x 6
   date           Y    X1     X5       X6 ID
   <date>     <dbl> <dbl>  <dbl>    <dbl> <chr>
 1 2016-10-21     1  4.14 0.0770 0.000429 CAT1
 2 2016-10-24     1  4.14 0.0793 0.000424 CAT1
 3 2016-10-25     0  4.16 0.0804 0.000404 CAT1
 4 2016-10-26     0  4.16 0.0626 0.000426 CAT1
 5 2016-10-27     0  4.16 0.0587 0.000442 CAT1
 6 2016-10-28     0  4.11 0.0610 0.000440 CAT1
 7 2016-10-31     0  4.14 0.0704 0.000425 CAT1
 8 2016-11-01     1  3.98 0.0867 0.000417 CAT1
 9 2016-11-02     0  6.00 0.0673 0.000453 CAT1
10 2016-11-03     0  8.42 0.0877 0.000429 CAT1
# ... with 39 more rows

Data:
df <- structure(list(date = structure(c(17095, 17098, 17099, 17100,
17101, 17102, 17105, 17106, 17107, 17108, 17109, 17112, 17113,
17114, 17115, 17116, 17119, 17120, 17121, 17122, 17123, 17126,
17127, 17128, 17130, 17133, 17134, 17135, 17136, 17137, 17140,
17141, 17142, 17143, 17144, 17147, 17148, 17149, 17150, 17151,
17154, 17155, 17156, 17157, 17158, 17162, 17163, 17164, 17165
), class = "Date"), Y = c(1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1,
1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1,
1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0), X1 = c(4.13858526513854,
4.13858526513855, 4.16341131085939, 4.16341131085937, 4.16341131085937,
4.11423119297315, 4.13858526513857, 3.97599968560627, 5.99758130881283,
8.41953801047614, 7.95231443679086, 7.88558780320248, 7.6408950559188,
4.51370117323327, 4.52868963859669, 4.39998987943623, 4.18852747359839,
4.27042958796773, 2.30720560360487, 2.3083029424251, 2.3083029424251,
2.30720560360488, 2.30720560360486, 2.33467572807035, 2.33467572807036,
2.33467572807036, 2.30720560360486, 2.33467572807035, 2.31545097851707,
2.3399413414153, 2.40685890963718, 2.40309440701756, 2.33633188340289,
2.3363318834029, 2.23996107961566, 2.23996107961567, 2.23996107961566,
3.14644299189703, 3.1910343925295, 3.4393907031427, 3.30413087760388,
3.33080017630688, 2.63827508869038, 2.99443088216722, 2.99443088216723,
2.99443088216722, 2.99443088216722, 3.14542139469794, 3.14542139469794
), X2 = c(18, 17, 16, 15, 14, 13, 12, 11, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19,
18, 17, 16, 15, 14, 13, 12, 11, 10, 10, 10, 10, 10, 87, 86, 85,
84, 83, 82, 81), X3 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), X4 = c(1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1), X5 = c(0.0769944316144198, 0.0793311568823971, 0.0803743155230278,
0.0625789438826206, 0.0586506192715035, 0.0610303101083243, 0.0703939970975855,
0.0867098273608016, 0.0673120522212106, 0.0877296725069155, 0.0923742675361241,
0.0821050363880187, 0.0360531976099817, 0.063410467337928, 0.0289807505667197,
0.0403890038946993, 0.0587200889534704, 0.124855015077667, 0.0636602113103218,
0.0748772236055617, 0.0828248414842617, 0.0588561607897347, 0.0437146614571738,
0.0399432627968126, 0.0535895503558405, 0.0538598239712004, 0.0600971764378981,
0.0543532803438423, 0.0412119504402689, 0.0445593481900316, 0.0471324573693227,
0.0366910541674913, 0.0412784111781792, 0.0338162772274317, 0.0391189676384125,
0.051151724195942, 0.0442197922283997, 0.0458769828703159, 0.0392536462039503,
0.0397989336000519, 0.0293505218180493, 0.0229058449521028, 0.0154019371887762,
0.0102366640366435, 0.00783792657548366, 0.014037818210456, 0.00900392496961011,
0.0148108452415051, 0.0159871581537364), X6 = c(0.000428660536007568,
0.000424348382531349, 0.000403672086504106, 0.000425772306880377,
0.000441567036819891, 0.000440420473928468, 0.000424616565866307,
0.000417156794102717, 0.000453102696396517, 0.000429420158272163,
0.000426339236438714, 0.000424204011080916, 0.000450812884669126,
0.00048728803860348, 0.000526461561504051, 0.000494106517096305,
0.000493488610269819, 0.00041740609044358, 0.000329604373072286,
0.000321981688032803, 0.000313820182149535, 0.000324018084037671,
0.000321928021838835, 0.000325886279909115, 0.000324905583026473,
0.000323263064904554, 0.000315582726878559, 0.00033115144688,
0.000326176783596685, 0.000320421043513733, 0.000317459171547033,
0.000306378296724892, 0.000304230982248009, 0.00031578316067723,
0.000315783239223671, 0.000308443856342272, 0.000302387474982801,
0.000302702247056619, 0.000300580888361005, 0.000298662388842681,
0.000295031270763261, 0.000300955138678924, 0.000308009865186193,
0.00031004060452567, 0.000308580481883199, 0.000309081734643359,
0.000309347430761987, 0.00031610525741575, 0.000316030523318374
), ID = c("CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1",
"CAT1", "CAT1")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-49L))

Answer: One dplyr option could be:
df %>%
 select(starts_with("X")) %>%
 select_if(~ sum(. %% 1) != 0) %>%
 bind_cols(df %>%
            select(-starts_with("X")))

      X1     X5       X6 date           Y ID
   <dbl>  <dbl>    <dbl> <date>     <dbl> <chr>
 1  4.14 0.0770 0.000429 2016-10-21     1 CAT1
 2  4.14 0.0793 0.000424 2016-10-24     1 CAT1
 3  4.16 0.0804 0.000404 2016-10-25     0 CAT1
 4  4.16 0.0626 0.000426 2016-10-26     0 CAT1
 5  4.16 0.0587 0.000442 2016-10-27     0 CAT1
 6  4.11 0.0610 0.000440 2016-10-28     0 CAT1
 7  4.14 0.0704 0.000425 2016-10-31     0 CAT1
 8  3.98 0.0867 0.000417 2016-11-01     1 CAT1
 9  6.00 0.0673 0.000453 2016-11-02     0 CAT1
10  8.42 0.0877 0.000429 2016-11-03     0 CAT1

Answer: Here is an option using purrr's imap as subsetting vector (original order of columns is preserved).
library(purrr)

not_whole <- function(x,y) {
      if (all(is.numeric(x)) & y != "Y") {
        !all(as.integer(x) == x)
      } else TRUE
    }

df[imap_lgl(df, ~ not_whole(.x, .y))]


Drupal quiz permissions per role

Question: How can I set Drupal role to a quiz? I will have more quizez and more roles. Each roles has its quiz.
I have a Drupal 7 with quiz module.
I tried different approaches using node acces and acl and did not work.
Answer: If node access and/or acl are not sufficient to fit your needs, you should have a look at the Group module, either for D7, or D8. To get an idea of what is possible with this module, checkout some of my answers about questions tagged with group.
In your case, if you just assign each quiz to an appropriate group, the magic included with the Group module should help you to implement what your question is about.

Drupal quiz permissions per role


How to store multiple values in a profile field (specific to each users)?

Question: I have a profile filed called Amount corresponding to each users, and I want to store multiple values in the profile field Amount. Bu using the custom module, the value of Amount is to be inserted on each registration (or it can be inserted any time by a cron program).
For example User Williams has Value 100 in his Amount field. This is not a single value, it may be
<code>Time    Amount
-------+------
Hour 1 :  10
Hour 2 :  30
Hour 3 :  20
Hour 4 :  35
Hour 5 :   5
============
Total  : 100
</code>
I want to display the total amount and individual amount by exact time.
How to do it?
Answer: You might be best installing content profile as this will allow you to use cck, when creating the profile field amount you can set it to unlimited which would allow you to store multiple values in an array for a single field.
The total would be another dynamic field which is calculated by adding the values from the amount array each time they are changed via a hook
To add the field to a content profile, follow the below steps:

Edit the profile content type
Go to manage fields
Add a new "text field
Fill in field details
On this page you will see a "number of values" option, choose "unlimited"

You can update content profile fields as below, just put it in a custom module or something:
<code>//load current user
global $user;
//find the users content profile
$uid = $user->uid;
$node = content_profile_load(profile,$uid);
$pid = $node->nid;
$node = node_load($pid);
//update the field AMOUNT
$node->field_AMOUNT[0]['value'] = $var1;
$node->field_AMOUNT[1]['value'] = $var2;
//do the same for the TOTAL with calculations
$node->field_TOTAL[0]['value'] = $var1 + $var2;
//save the content profile node
node_save($node);
</code>
Comment: Thanks, I have installed the **Content Profile** module, and when I create the profile field **Amount**, I couldn't see the **Unlimited** option.  Please explain your words **which would allow you to store multiple values in an array for a single field** in detail.
Comment: Updated my answer
Comment: I think some error in your code, that's why I used this one,                                        **`$node = new stdClass();
$node->type = 'profile';
$node->title = 'This is a title';
$node->body = 'This is the body.';
$node->teaser = 'This is the teaser.';
$node->uid = 25;
$node->status = 0;
$node->promote = 1;
$var1 = 1753;
$node->field_amount[0]['value'] = $var1;
node_save($node);`**
Answer: You need to create a new profile type in "drupal/module/profile.module" that is called "total."
Comment: But this field can store only one value, i want to store multiple values like the table above


How to get and pass user conversation data to backend API in Azure Bot Framework Composer v1.4.0?

Question: I am new to Azure Bot Framework Composer.
I am creating chatbot using Azure Bot Framework Composer(without writing code) which need to be launch in Angular Web Application from Azure Cloud after deployment.
In my chatbot project, need to fetch the user conversation data and send it to backend API from Azure Bot Framework Composer.
How to achieve this?, anyone help on this.
Answer: You can view the conversation data using ${conversation} scope and from dialog stack can access required information. To get last conversation data can use ${turn.Activity}. For sending to backend API, create HTTP post method and pass the data in the body of your request.
Answer: Without knowing more details on your chatbot project, one reasonable approach to take is to use the Send HTTP Request action in the composer.

After you obtain the values from the conversation that you are looking for, (can be stored in conversation / dialog variables), you could then pass it onto an HTTP send request and deliver that to your backend API.

With this, you can send any payload to your backend API.
Comment: my actual question is how to fetch the user conversation data from bot?
Comment: Then you need to revisit the Bot Framework documentation on how to handle the conversation flow and data:

https://learn.microsoft.com/en-us/composer/concept-memory
Comment: what visual tool is that?


mklink folders from Windows server to AIX

Question: We need to make a softlink to other server on AIX and when doing accordingly to guide in cmd occurs an error
mklink /D \\10.0.0.10\folder C:\folder

and it returns - The request is not supported
But vice versa
mklink /D C:\folder \\10.0.0.10\folder

it says - symbolic link created for C:/folder .......
What can be the issue here ?
Answer: symlink is a filesystem feature. The windows command mklink can only create it on a NTFS filesystem (and maybe ReFS, I don't have experience with that).
A remote filesystem (especially one that is served by a unix system) is not NTFS, so it can't create a symlink there.
Sadly, the official documentation lacks any information about that, but the Wikipedia page is pretty detailed.


Why do we assume that the error is normally distributed?

Question: I wonder why do we use the Gaussian assumption when modelling the error. In Stanford's ML course, Prof. Ng describes it basically in two manners:

It is mathematically convenient. (It's related to Least Squares fitting and easy to solve with pseudoinverse)
Due to the Central Limit Theorem, we may assume that there are lots of underlying facts affecting the process and the sum of these individual errors will tend to behave like in a zero mean normal distribution. In practice, it seems to be so.

I'm interested in the second part actually. The Central Limit Theorem works for iid samples as far as I know, but we can not guarantee the underlying samples to be iid.
Do you have any ideas about the Gaussian assumption of the error?
Comment: I asked the question for the general case. Most of the stories start with Gaussian error assumption. But, personally, my own interest is matrix factorizations and linear model solutions (so say regression).
Comment: What setting are you talking about? Classification, regression, or something more general?
Comment: Relevant:  http://stats.stackexchange.com/questions/120776/why-should-we-use-t-errors-instead-of-normal-errors/120787#120787
Answer: I think you've basically hit the nail on the head in the question, but I'll see if I can add something anyway. I'm going to answer this in a bit of a roundabout way ...
The field of Robust Statistics examines the question of what to do when the Gaussian assumption fails (in the sense that there are outliers):

it is often assumed that the data errors are normally distributed, at least approximately, or that the central limit theorem can be relied on to produce normally distributed estimates. Unfortunately, when there are outliers in the data, classical methods often have very poor performance

These have been applied in ML too, for example in Mika el al. (2001) A Mathematical Programming Approach to the Kernel Fisher Algorithm, they describe how Huber's Robust Loss can be used with KDFA (along with other loss functions). Of course this is a classification loss, but KFDA is closely related to the Relevance Vector Machine (see section 4 of the Mika paper).
As implied in the question, there is a close connection between loss functions and Bayesian error models (see here for a discussion).
However it tends to be the case that as soon as you start incorporating "funky" loss functions, optimisation becomes tough (note that this happens in the Bayesian world too). So in many cases people resort to standard loss functions that are easy to optimise, and instead do extra pre-processing to ensure that the data conforms to the model.
The other point that you mention is that the CLT only applies to samples that are IID. This is true, but then the assumptions (and the accompanying analysis) of most algorithms is the same. When you start looking at non-IID data, things get a lot more tricky. One example is if there is temporal dependence, in which case typically the approach is to assume that the dependence only spans a certain window, and samples can therefore be considered approximately IID outside of this window (see for example this brilliant but tough paper Chromatic PAC-Bayes Bounds for Non-IID Data: Applications to Ranking and Stationary β-Mixing Processes), after which normal analysis can be applied.
So, yes, it comes down in part to convenience, and in part because in the real world, most errors do look (roughly) Gaussian. One should of course always be careful when looking at a new problem to make sure that the assumptions aren't violated.
Comment: Another convenience item associated with normally distributed data is that 0 correlation implies independence.
Comment: The comment about IID-ness isn't quite right. There are (several) very general Central Limit Theorems that apply when results are independent but not identically distributed; see e.g. the Lindeberg CLT. There are also CLT results that don't even need independence; they can arise from exchangeable observations, for example.
Comment: +1 Thank you very much especially for mentioning about robust and non-robust statistics. I do observe that median and alpha-trimmed mean works usually better than mean in practice but I didn't know the theory behind them.


Uma função que imprima dinamicamente todos os elementos da lista na ordem inversa [1, 2, 3, 5, 8, 13, 21, 34, 55, 89],

Question: UMA FUNCÃO EM DART PARA IMPRIMIR OS ELEMENTO NA ORDEM INVERSA
NO CASO ELA ESTARIA ASSIM 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 ESTOU QUERENDO QUE FIQUE ASSIM
89,55, 34, 21, 13, 8, 5, 3, 2, 1 REVERSO
Comment: Opa tudo bem? Parece que você é novo por aqui, da uma olhadinha nesses tópicos [Pergunta sem muita qualiade](https://pt.stackoverflow.com/help/quality-standards-error) e [Como fazer uma boa pergunta](https://pt.stackoverflow.com/help/how-to-ask).
Comment: Pelo seu nome de usuário acredito que já conheça o [DartPad](https://dartpad.dev/), correto? Sabia que lá existe alguns atalhos no editor? Por exemplo, crie uma propriedade da seguinte forma `List lista = [1, 2, 3];`, depois na linha de baixo digite `lista.` e com o cursor na frente do "ponto" aperte CTRL+Espaço... Você verá todos os métodos úteis que tal tipo lhe dispõe, com isso você encontrará o que quer...
Answer: A intrução for serve para repetir processos até que a variável declarada chegue ao limite declarado.
void main() {
    List<int> lista = [1, 2, 3, 5, 8, 13, 21, 34, 55, 89];

    for (int i = lista.length; i > 0; i--) {
        print(lista[i - 1]);
    }

}

// Saída: 89 55 34 21 13 8 5 3 2 1

Veja que iniciamos a variável i com o valor da quantidades de elementos que o vetor lista possui (i = lista.lenght);
A seguir, definimos até quando o for repetirá, no caso ele repetirá enquanto i for maior que 0 (i > 0);
E, por último, definimos que a cada repetição do for, o valor de i se subtrai em um (i--ou i = i - 1).
O mesmo princípio pode ser usado para aumentar, porém se inverte os valores:
void main() {
    List<int> lista = [1, 2, 3, 5, 8, 13, 21, 34, 55, 89];
    for (int i = 0; i < lista.length; i++) {
        print(lista[i]);
    }
}

// Saída: 1 2 3 5 8 13 21 34 55 89

Comment: A questão é que o AP não nos deu nenhum exemplo do que já tentou até agora e jogou uma "pergunta" genérica e de qualquer jeito, que como você disse "Parece uma questão acadêmica" e provavelmente é, pois existe outra pergunta idêntica dele que foi fechado por não atender os critérios mínimos de qualidade do site . Aconselho que você leia [Manual de como NÃO fazer perguntas](https://pt.meta.stackoverflow.com/a/5486/87785).
Comment: A sua reposta é legal, mas já existe uma função própria para isso nos tipos `List` que em uma linha você resolve o problema de inverter uma lista.
Comment: O problema é que na questão não é mencionado, mas isso parece ser uma questão acadêmica, e caso esteja começando em Dart é bom pegar os princípios básicos dos laços de repetição também
Comment: Obrigado @JeanFranz pela dica.. na verdade eu estou tendo meus primeiros contatos com Dart, eu entrei nessa plataforma recentemente, em busca de ajuda, até peço desculpa por não ter elaborado uma pergunta melhor.. estou aprendendo ainda usar essa plataforma!! e confesso que já recebi muitos comentários abusivos aqui.. pensava que esse site era para aluno tbm, mais pelo visto só tem doutores.. agradeço por responder minha pergunta deu certo ...


If $a,b,c$ are sides of a triangle, then find range of $\frac{ab+bc+ac}{a^2+b^2+c^2}$

Question: $$\frac{ab+bc+ac}{a^2+b^2+c^2}$$
$$=\frac{\frac 12 ((a+b+c)^2-(a^2+b^2+c^2))}{a^2+b^2+c^2}$$
$$=\frac 12 \left(\frac{(a+b+c)^2}{a^2+b^2+c^2}-1\right)$$
For max value, $a=b=c$
Max =$1$
How do I find the minimum value
Comment: The minimal value is not attained, the infimum is $1/2$, if $a=b$ and $c) is small, say.
Comment: If those are sides of a triangle, $a,b,c>0$ and $a+b>c,\ b+c>a,\ a+c>b$
Comment: @ProfessorVector you are right, could you explain the process?
Answer: In $\Delta ABC$, $a^2=b^2+c^2-2bc \cos A > b^2+c^2-2bc$.
Adding up the similar inequalities gives $ab+bc+ca > \frac{1}{2} (a^2+b^2+c^2)$
Comment: You don't have to use the cosine rule because $a+c>b\implies a>b-c\implies a^2>b^2+c^2-2bc$, as I mentioned in the comments. It's just a basic triangle inequality.
Comment: Yes it's just a matter of whether triangle inequality or cosine rule is more elementary :)
Comment: Well, I heard of the triangle inequality in the elementary school, whereas trigonometry is taught in highschool (in my country). (:
Answer: Since we have triangle sides, we better use $$a=u+v,\quad b=v+w,\quad c=w+u$$ with $u,v,w>0.$ Arithmetically, $u=(a+c-b)/2, v=(a+b-c)/2, w=(b+c-a)/2,$ so taking $u,v,w>0,$ the triangle inequality is satisfied, automagically. Geometrically speaking, $u,v,w$ are the segments the sides are divided into by the touch-down points of the incircle.
Then, your expression becomes (after some simple algebra)
$$\frac{1+3\,p/s}{2\,(1+p/s)},$$ where $s=u^2+v^2+w^2$ and $p=uv+vw+wu.$ Obviously, $0<p\le s,$ so the range is $\left(\frac12,1\right].$
Comment: You could've use the triangle inequality to find the infimum. (:


Find $\lim_\limits{n\to \infty}\{en!\}$.

Question: Find the limit $\lim_\limits{n\to \infty}\{en!\}$.
$Attempt:$ $\lim_\limits{n\to \infty}\{en!\}=\lim_\limits{n\to \infty}\{(1+{1\over 1!}+{1\over 2!}+{1\over 3!}+...+{1\over n!}+...)n!\}$. The fraction part is what confuses me. Also I don't know if it has something to do with the convergence of $\sum n!$. I would appreciate your help.
Comment: @Henrik I think that the curly braces around $en!$ mean "fractional part".
Comment: $n!$ goes to infinity as $n$ goes to infinity, multiplying by $e$ doesn't change that. Are you sure this is what you wanted to ask?
Answer: You can write $e = \displaystyle \sum_{k=0}^\infty \frac{1}{k!}$ so that $$en! =  \sum_{k=0}^\infty \dfrac{n!}{k!} = \sum_{k=0}^n \frac{n!}{k!} + \sum_{k=n+1}^\infty \frac{n!}{k!}$$ so that \begin{align*}\{en!\} &= \frac{1}{n+1} + \frac{1}{(n+1)(n+2)} + \frac{1}{(n+1)(n+2)(n+3)} + \cdots \\ &\le \frac{1}{n+1} + \frac{1}{(n+1)^2} + \frac{1}{(n+1)^3} + \cdots \\ &= \frac 1n \end{align*}
where the last equality comes from the geometric series formula. Thus $\{en!\} \to 0$.
Comment: If you have $x = k + \alpha$ where $k \ge 0$ is an integer and $0 < \alpha < 1$ then $\{x\} = \alpha$. The series $$\sum_{k=0}^n \frac{n!}{k!}$$ is an integer.
Comment: I can't understand how the complete part disappeared...
Comment: What do you mean by "complete part"?
Comment: The floor part...
Comment: That is what we're looking at, isn't it?
Comment: So that's what I took... Not the fraction part..
Comment: Oh wait. I am sorry, I was mixed up. Thank you.
Comment: Suppose I don't know the last equality. Can't I just see that everything becomes zero?
Comment: What about $\sum_{k=0}^n \frac{n!}{k!}$ ?
Comment: This is not the fractional part... We don't need it
Comment: Nevermind.${}{}{}$


Generating n random numbers between i and j python

Question: I want to create a random number using n numbers which are between i and j. For instance, for n=10 and i=1 and j=5, such an output is expected: 2414243211. I did it in R using this code:
paste(floor(runif(10,1,5)),collapse="") #runif create 10 random number between 1 and 5 and floor make them as integer and finally paste makes them as a sequence of numbers instead of array.

I want to do the same in Python. I found random.uniform but it generates 1 number and I don't want to use loops.
import random
import math
math.floor(random.uniform(1,5)) #just generate 1 number between 1 and 5

update:

i and j are integers between 0 and 9, while n could be any integer.
i and j decide which number can be used in the string while n indicates the length of the numeric string.
Comment: In native Python you'll have to use a loop - if you're familiar with R - then you might want to look at numpy/pandas.
Comment: So you want one number with those digits?  `int(''.join([str(math.floor(random.uniform(1,5))) for _ in range(10)]))`
Answer: If I understand your question (not sure I do), and you have Python 3.6, you can use random.choices:
>>> from random import choices
>>> int(''.join(map(str, choices(range(1, 5), k=10))))
2121233233

Answer: If you consider list-comprehensions being loops (which they actually in many ways are) there you will not be satisfied with this but I will try my luck:
from random import randint

res = ''.join([str(randint(1, 5)) for _ in range(10)])
print(res)  #-> 4353344154

Notes:

The result is a string! If you want an integer, cast to int.
randint works incluselively; the start (1) and end (5) might be produced and returned. If you do not want that, modify them (start = 2 and end = 4)
Is there a reason you are using random.uniform (and subsequently math.floor()) and not simply randint?
Comment: Since I want to create thousands of these numbers with different lengths, I prefer to use loops for that purpose and not for generating random numbers. So there is no way to generate more than 1 number without loops? For 3, no, I simply was not aware of `randint`. Thank you very much
Comment: @Hadij There is. just wrap the existing list-comprehension in another one. `res = [''.join([str(randint(1, 5)) for _ in range(10)]) for _ in range(5)]`
Answer: The random.choices() function does what you want:
>>> from random import choices
>>> n, i, j = 10, 1, 5
>>> population = list(map(str, range(i, j+1)))
>>> ''.join(choices(population, k=n))
'5143113531'

Comment: That's pretty much what I did @JonClements but Raymond is there a reason to do the string conversion *before sampling with choices?
Comment: It's hard to tell which is best from the question as worded.  My take was that *i* and *j* were digit ranges so there would be ten or fewer of them.  Meanwhile, it looked like *n* could get large.
Comment: @RaymondHettinger Yes, You are right. i and j are in this range [0,9] and n could be any integer. thank you.
Comment: I'd go for: `''.join(map(str, choices(range(i, j+1), k=n)))` - no need to make the range a list, just let it sample from the range, and have that mapped. Might just be worth noting that `choices` is 3.6+ - still a lot of people on 3.5...
Comment: Thinking about cases where `range(1, 1000000000)` could be the candidate pool and `k=10` is all that's required... at least you'll only have a list of 10 elements instead of a temporary one of the range...
Comment: @Chris_Rands oh - so you did - sorry I didn't see that while commenting on Raymond's answer...
Comment: @Hadij  Given that interpretation, can you mark this as answer as the better solution.
Answer: x = ''.join([str(math.floor(random.uniform(1,5)))  for i in range(10)])


t-distribution having heavier tail than normal distribution

Question: In my lecture notes it says,

t-distribution looks like normal, though with slightly heavier tails.

I understand why it would look normal (because of the Central Limit Theorem). But I am having hard time understanding how to mathematically prove that it has heavier tails than the normal distribution and if there is a way to measure to what extent it's heavier than the normal distribution.
Answer: The first thing to do is to formalize what we mean by "heavier tail". One could notionally look at how high the density is in the extreme tail after standardizing both distributions to have the same location and scale (e.g. standard deviation):

(from this answer, which is also somewhat relevant to your question)
[For this case, the scaling doesn't really matter in the end; the t will still be "heavier" than the normal even if you use very different scales; the normal always goes lower eventually]
However, that definition - while it works okay for this particular comparison - doesn't generalize very well.
More generally, a much better definition is in whuber's answer here. So if $Y$ is heavier-tailed than $X$, as $t$ becomes sufficiently large (for all $t>$ some $t_0$), then $S_Y(t)>S_X(t)$, where $S=1-F$, where $F$ is the cdf (for heavier-tailed on the right; there's a similar, obvious definition on the other side).

Here it is on the log-scale, and on the quantile scale of the normal, which allows us to see more detail:

So then the "proof" of heavier tailedness would involve comparing cdfs and showing that the upper tail of the t-cdf eventually always lies above that of the normal and the  lower tail of the t-cdf eventually always lies below that of the normal.
In this case the easy thing to do is to compare the densities and then show that the corresponding relative position of the cdfs (/survivor functions) must follow from that.
So for example if you can argue that (at some given $\nu$)
$  x^2 - (\nu+1) \log(1+\frac{x^2}{\nu}) > 2\cdot\log(k)\qquad^\dagger$
for the necessary constant $k$ (a function of $\nu$), for all $x>$ some $x_0$, then it would be possible to establish a heavier tail for $t_\nu$ also on the definition in terms of bigger $1-F$ (or bigger $F$ on the left tail).
$^\dagger$ (this form follows from the difference of the log of the densities, if that holds the necessary relationship between the densities holds)
[It's actually possible to show it for any $k$ (not just the particular one we need coming from the relevant density normalizing constants), so the result must hold for the $k$ we need.]
Comment: A graph with $\log S(x)$ (and perhaps extending $x$ a little) might demonstrate the heavier tails more clearly, and could also work with higher degrees of freedom,
Comment: @Henry I generated such a plot but wasn't sure how much value it added so I didn't include it. I'll think about putting it in.
Comment: @Henry I included the plot.
Answer: One way to see the difference is by use of moments $E\{x^n\}.$
"Heavier" tails will mean higher values for the even power moments (power 4, 6, 8), when variance is the same.  In particular, the 4-th order moment (around zero) is called kurtosis and compares in some exact sense the heaviness of the tails.
See Wikipedia for details (https://en.wikipedia.org/wiki/Kurtosis)
Comment: @Henry Nevertheless this idea is good. Expanding the CDF of the Student $t(\nu)$ distribution around $+\infty$ shows it is asymptotically proportional to $x^{-\nu}$. Thus all absolute moments of weight less than $\nu$ exist and all absolute moments of weight greater than $\nu$ diverge. With the Normal distribution, all absolute moments exist. This provides a definite ordering of the tails of all Student $t$ distributions and of the Normal distribution. In effect, the parameter $\nu$ provides one answer to the original question about how to measure the heaviness of a tail.
Comment: Though for a $t$-distribution with $3$ or $4$ degrees of freedom, the kurtosis is infinite, while with $2$ degrees of freedom the standard deviation is infinite so you cannot calculate the kurtosis, and with $1$ degree of freedom you cannot even calculate the mean or the $4$th moment.
Answer: Here is a formal proof based on the survival functions. I use the following definition of "heavier tail" inspired by wikipedia:
A random variable $Y$ with survival function $S_y(t)$ has heavier tails than a random variable $X$ with survival function $S_x(t)$ iff
$$\lim_{t\to\infty}\frac{S_y(t)}{S_x(t)} = \infty$$
Consider a random variable $Y$ distributed as Student's t with mean zero, degrees of freedom $\nu$ and scale parameter $a$. We compare this to the random variable $X\sim\mathcal{N}(0,\sigma^2)$. For both variables, the survival functions are differentiable. Therefore,
\begin{align*}
\lim_{t\to\infty}\frac{S_y(t)}{S_x(t)} &= \lim_{t\to\infty}\frac{f_y(t)}{f_x(t)} = \exp \lim_{t\to\infty}\left(\log f_y(t) - \log f_x(t)\right)\\
&=\exp \lim_{t\to\infty}\left(-\frac{\nu+1}{2}\log\left(1+\frac{t^2}{\nu a^2}\right) - \left(-\frac{1}{2\sigma^2}t^2\right)+C\right)\\
&=\exp\left(\lim_{t\to\infty}-\frac{\nu+1}{2}\log\left(1+\frac{t^2}{\nu a^2}\right) - \left(-\frac{1}{2\sigma^2}t^2\right)+C\right)\\
&=\exp\left(\lim_{t\to\infty}\frac{1}{2\sigma^2}t^2-\frac{\nu+1}{2}\log\left(1+\frac{t^2}{\nu a^2}\right)+C\right)\\
&=\exp\left(\frac{1}{2}\lim_{u\to\infty}\frac{a^2}{\sigma^2}u - (\nu+1)\log\left(1+\frac{u}{\nu}\right)+C\right)\\
&=\exp\left(\frac{1}{2}\lim_{u\to\infty}u\left(\frac{a^2}{\sigma^2} - \frac{(\nu+1)\log\left(1+\frac{u}{\nu}\right)}{u}+\frac{C}{u}\right)\right)
\end{align*}
Where we have substituted $u=t^2/a^2$. Note that $0<a^2/\sigma^2<\infty$ is a constant, $\lim_{u\to\infty} C/u = 0$ and
$$\lim_{u\to\infty} \frac{(\nu+1)\log\left(1+\frac{u}{\nu}\right)}{u} = \lim_{u\to\infty} \frac{(\nu+1)}{(1)(1+\frac{u}{\nu})(\nu)} = 0$$
Hence by algebraic limit theorem,
$$\lim_{t\to\infty}\frac{S_y(t)}{S_x(t)} = \exp\left(\frac{1}{2}\lim_{u\to\infty} u\left(\frac{a^2}{\sigma^2} - (0) + (0)\right)\right) = \infty$$
Importantly, the result holds for arbitrary (finite) values of $a$, $\sigma^2$, and $\nu$, so you can have situations where a t distribution has smaller variance than a normal, but still having heavier tails.
Comment: Just a note that this "definition" of heavier tails is not always acceptable. For example, the N(0,1) distribution, by this definition, has heavier tails than the .9999*U(-1,1) + .0001*U(-1000, 1000) distribution, even though the latter distribution produces occasional values up to 175 standard deviations from the mean, despite having bounded support. Of course, the N(0,1) also produces such values, but with probabilities well below what can be considered relevant for practical purposes.


Cost complexity pruning and prediction error

Question: I am reading the book titled "An Introduction to Statistical Learning" by James et al. There it is mentioned on page 309 that we pick the cost complexity parameter α  to minimize the average Mean Squared Prediction Error.
Then on page 326, the following is mentioned:

"We use the argument FUN=prune.misclass in order to indicate that we
  want the classification error rate to guide the cross-validation and
  pruning process, rather than the default for the cv.tree() function,
  which is deviance."

My question is when using cross-validation to choose α, which criterion should we use then?  Average Mean Squared Prediction Error or Classification error rate? It seems that at one place it is mentioned that we need to look at the average mean squared prediction error and at another place it is mentioned that we need to use classification error.
Answer:
It seems that at one place it is mentioned that we need to look at the average mean squared prediction error and at another place it is mentioned that we need to use classification error.

The reason is that pages 304-310 explain regression trees. That is why it talks about using Mean Squared Prediction Error as the evaluation metric. There is no classification error rate for a regression problem.
Then from page 311-314, it explains about Classification Trees. The excerpt from page 326 which you have quoted is under the section 'Fitting Classification Trees'. That is why it uses Classification error rate. You can use classification error rate to improve your model in case of classification problems and similarly you can use Mean Squared Prediction Error in case of regression problems.


What size should the cpu limits be decided in k8s?

Question: I did performance test to my application.
The usage of cpu was 30% while testing. - the test sever has 4 cores.
so I thought one core had 30/4 usage = 7.5%.
I want to a container in k8s to keep the usage of CPU under 30%
so I decided that the cpu limits was 250m + 50m(extra core).
I am wondering if this way is right? otherwise, is there the best other way to decide the limit of cpu?
Comment: for experience it is right, you just limit your cpu of resource 300m. max usage should limit to one core 30 percent. @seo
Answer: To answer your question, I am wondering if this way is right?, yes, that's the right way. You can also use a tool called goldilocks, a kubernetes controller that collects data about running pods and provides recommendations on how to set resource requests and limits. It could help you identify a starting point for resource requests and limits.

I'm not sure if your calculations are correct, as CPU resources are defined in millicores. If your container needs two full cores to run, you would put the value "2000m". If your container only needs ¼ of a core, you would put a value of "250m".
So if you want 30% of 1 of your cores then 300m is the correct amount. But if you want 30% of your 4 cores, then I would say 1200m is the correct amount.
There is kubernetes documentation about that.

You could also consider using Vertical Pod Autoscaler.

Vertical Pod Autoscaler (VPA) frees the users from necessity of setting up-to-date resource limits and requests for the containers in their pods. When configured, it will set the requests automatically based on usage and thus allow proper scheduling onto nodes so that appropriate resource amount is available for each pod. It will also maintain ratios between limits and requests that were specified in initial containers configuration.

I would also recommend to read below tutorials about limits and requests:

Setting the right requests and limits in Kubernetes
Kubernetes best practices: Resource requests and limits
Comment: I was also searching something like that, thanks for sharing :)
Comment: Thank you for sharing the wonderful answer. I am sorry for being late


How to add bytes together and validate them against the checksum in python

Question: How do you calculate the checksum of incoming bytes to see if it is a valid packet?
Currently I am reading the bytes and decoding them and receiving the information but before I do that I would like to be able to validate it against the checksum to make sure I do not get any invalid/corrupt packets.
Here is what I currently have
def batteryConnect(port, baudrate):
    # establishing a serial connection
    ser = serial.Serial(port=port, baudrate= baudrate, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE,timeout=3)
    return ser

class VictronBmv700:

    def __init__(self,port,baudrate):
        self.port = port
        self.baudrate = baudrate
        self.parameterDict = dict.fromkeys(["PID","V","I","P","CE","SOC","TTG","Alarm","Relay","AR","BMV","FW","H1","H2","H3",
                                  "H4","H5","H6","H7","H8","H9","H10","H11","H12","H17","H18"])

    def getBMVInfo(self):

        bmvdata = batteryConnect(self.port,self.baudrate)

        #getting the data
        bmvdata.flushInput()

        #getting the message then splitting the key, value pairs
        while True:
            print(bmvdata.readline())

            message = bmvdata.readline().decode(encoding='utf-8',errors='ignore')

            #splitting on tabs
            message_parts = message.split('\t')

            if len(message_parts) > 1:
                key = message_parts[0]
                value = message_parts[1].rstrip()  #stripping \r\n after the value

                #updating deictionary based on keys and their values.
                self.parameterDict[key] = value

if __name__ == "__main__":
    print("BATTERY MONITOR")
    bmv700 = VictronBmv700("COM17", 19200)
    bmv700.getBMVInfo()

Which starts outputting the following (from the print(bmvdata.readline()) )
b'\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd7\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd6\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'

How would I check the incoming bytes against the checksum then continue to decode after validation?
EDIT
Sometimes I get different checksum values as below.
Here are two additional times I ran the code.
b'\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd0\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd7\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd0\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd8\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd8\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd8\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd0\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd0\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd8\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd8\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd8\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd8\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd8\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd8\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd0\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd0\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd0\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd0\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'
b'PID\t0x203\r\n'
b'I\t0\r\n'
b'CE\t0\r\n'
b'TTG\t-1\r\n'
b'Relay\tOFF\r\n'
b'BMV\t700\r\n'
b'Checksum\t\xd0\r\n'
b'H2\t0\r\n'
b'H4\t0\r\n'
b'H6\t-9001\r\n'
b'H8\t28403\r\n'
b'H10\t0\r\n'
b'H12\t0\r\n'
b'H18\t87\r\n'

Second run
b'\r\n'
b'V\t25548\r\n'
b'P\t0\r\n'
b'SOC\t1000\r\n'
b'Alarm\tOFF\r\n'
b'AR\t0\r\n'
b'FW\t0310\r\n'
b'H1\t-6254\r\n'
b'H3\t0\r\n'
b'H5\t0\r\n'
b'H7\t-10\r\n'
b'H9\t0\r\n'
b'H11\t0\r\n'
b'H17\t20\r\n'
b'Checksum\tT\r\n'
b'V\t25549\r\n'
b'P\t0\r\n'
b'SOC\t1000\r\n'
b'Alarm\tOFF\r\n'
b'AR\t0\r\n'
b'FW\t0310\r\n'
b'H1\t-6254\r\n'
b'H3\t0\r\n'
b'H5\t0\r\n'
b'H7\t-10\r\n'
b'H9\t0\r\n'
b'H11\t0\r\n'
b'H17\t20\r\n'
b'Checksum\tT\r\n'
b'V\t25548\r\n'
b'P\t0\r\n'
b'SOC\t1000\r\n'
b'Alarm\tOFF\r\n'
b'AR\t0\r\n'
b'FW\t0310\r\n'
b'H1\t-6254\r\n'
b'H3\t0\r\n'
b'H5\t0\r\n'
b'H7\t-10\r\n'
b'H9\t0\r\n'
b'H11\t0\r\n'
b'H17\t20\r\n'
b'Checksum\tT\r\n'
b'V\t25548\r\n'
b'P\t0\r\n'
b'SOC\t1000\r\n'
b'Alarm\tOFF\r\n'
b'AR\t0\r\n'
b'FW\t0310\r\n'
b'H1\t-6254\r\n'
b'H3\t0\r\n'
b'H5\t0\r\n'
b'H7\t-10\r\n'
b'H9\t0\r\n'
b'H11\t0\r\n'
b'H17\t20\r\n'
b'Checksum\tT\r\n'
b'V\t25549\r\n'
b'P\t0\r\n'
b'SOC\t1000\r\n'
b'Alarm\tOFF\r\n'
b'AR\t0\r\n'
b'FW\t0310\r\n'
b'H1\t-6254\r\n'
b'H3\t0\r\n'
b'H5\t0\r\n'
b'H7\t-10\r\n'
b'H9\t0\r\n'
b'H11\t0\r\n'
b'H17\t20\r\n'
b'Checksum\tT\r\n'
b'V\t25548\r\n'
b'P\t0\r\n'
b'SOC\t1000\r\n'
b'Alarm\tOFF\r\n'
b'AR\t0\r\n'
b'FW\t0310\r\n'
b'H1\t-6254\r\n'
b'H3\t0\r\n'
b'H5\t0\r\n'
b'H7\t-10\r\n'
b'H9\t0\r\n'
b'H11\t0\r\n'
b'H17\t20\r\n'
b'Checksum\tT\r\n'
b'V\t25548\r\n'
b'P\t0\r\n'
b'SOC\t1000\r\n'
b'Alarm\tOFF\r\n'
b'AR\t0\r\n'
b'FW\t0310\r\n'
b'H1\t-6254\r\n'
b'H3\t0\r\n'
b'H5\t0\r\n'
b'H7\t-10\r\n'
b'H9\t0\r\n'
b'H11\t0\r\n'
b'H17\t20\r\n'
b'Checksum\tT\r\n'
b'V\t25548\r\n'
b'P\t0\r\n'
b'SOC\t1000\r\n'
b'Alarm\tOFF\r\n'
b'AR\t0\r\n'
b'FW\t0310\r\n'
b'H1\t-6254\r\n'
b'H3\t0\r\n'
b'H5\t0\r\n'
b'H7\t-10\r\n'
b'H9\t0\r\n'
b'H11\t0\r\n'
b'H17\t20\r\n'
b'Checksum\tT\r\n'
b'V\t25548\r\n'
b'P\t0\r\n'
b'SOC\t1000\r\n'
b'Alarm\tOFF\r\n'
b'AR\t0\r\n'
b'FW\t0310\r\n'

EDIT
Ran the following code
current_block = []
# it's unfortunate that lines start with `\r\n` rather than end with it
# we will read the first empty line separatey, and then include these
# 2 characters in the last line that is read
message = bmvdata.readline()
while True:
    # Note that we cannot decode yet, since the Checksum value may not be a valid utf8 character
    message = bmvdata.readline()

    # we save the message in the current block before any preprocessing
    current_block.append(message)

    #splitting on tabs (as bytes !!)
    message_parts = message.split(b'\t')

    if len(message_parts) > 1:
        key = message_parts[0].decode('utf8')  # here it is safe to decode the key
        value = message_parts[1].rstrip()  #stripping \r\n after the value

    if key == 'Checksum':
        block_chars = b''.join(current_block)
        checksum = sum(block_chars) % 256
        print('Retrieved  checksum', value[-1])
        print('Calculated checksum', checksum)
        print('----')

        # reset the block
        current_block = []

output
Retrieved checksum 84
Calculated checksum 0
-------
Retrieved checksum 219
Calculated checksum 0
-------
Retrieved checksum 84
Calculated checksum 0
-------
Retrieved checksum 220
Calculated checksum 0
-------
Retrieved checksum 84
Calculated checksum 0
-------
Retrieved checksum 219
Calculated checksum 0
-------
Retrieved checksum 84
Calculated checksum 0
-------
Retrieved checksum 219
Calculated checksum 0
-------
Retrieved checksum 84
Calculated checksum 0
-------
Retrieved checksum 220
Calculated checksum 0
-------
Retrieved checksum 84
Calculated checksum 0
-------
Retrieved checksum 220
Calculated checksum 0
-------
Retrieved checksum 84
Calculated checksum 0
-------
Retrieved checksum 220
Calculated checksum 0
-------
Retrieved checksum 84
Calculated checksum 0
-------
Retrieved checksum 219
Calculated checksum 0
-------
Retrieved checksum 84
Calculated checksum 0
-------

Comment: Can you add a file with a lot more rows, please, so that we can check whther my assumption is true? (Pastebin of github Gist )
Comment: Thanks for accepting the answer. I feel we have not fully resolved your problem. Did you manage to do a correct checksum ?
Comment: I have not had time to check/run the most recent code that you posted. I will likely get to it in a few days and let you know!
Comment: @CiprianTomoiagă I ran your code from the answer and posted the results to the question. Does that seem right? The calculated checksum = 0 ?  I see the retrieved checksum varies from 220,84, 219.
Comment: indeed, that is exactly what the documentation is saying (see section Data Integrity)! If the calculated checksum is 0 then the trandmission was OK. You can ignore "retrieved checksum", it was there only because I was getting mixed results on your samples
Answer: It seems that the poster is trying to read data from a BMV700 battery monitor. Reading the papers here, we see that this communicates over the Serial interface using a text protocol:

2 TextProtocol
When no VE.Direct queries are sent to the device, the charger periodically sends human readable (TEXT) data to the serial port. See the "VE.Direct Protocol" document for a detailed description of the contents and availability of the information

Excerpt from the HEX protocol (BMV-7xx-HEX-Protocol-public.pdf)
Looking into the specification of the TEXT protocol (VE.Direct-Protocol-3.28.pdf), we find:

Message format
The device transmits blocks of data at 1 second intervals. Each field is sent using the following format:
<Newline><Field-Label><Tab><Field-Value>

The identifiers are defined as follows:
+---------------+--------------------------------------------------------------------------------------+
| Identifier    | Meaning                                                                              |
+===============+======================================================================================+
| <Newline>     | A carriage return followed by a line feed (0x0D, 0x0A).                              |
+---------------+--------------------------------------------------------------------------------------+
| <Field-Label> | An arbitrary length label that identifies the field.                                 |
|               | Where applicable, this will be the same as the label that is used on the LCD.        |
+---------------+--------------------------------------------------------------------------------------+
| <Tab>         | A horizontal tab (0x09).                                                             |
+---------------+--------------------------------------------------------------------------------------+
| <Field-Value> | The ASCII formatted value of this field.                                             |
|               | The number of characters transmitted depends on the magnitude and sign of the value. |
+---------------+--------------------------------------------------------------------------------------+

This corrsponds to the data you're printing, with one exception: the line starts with \r\b, it doesn't end with them.
Then, the file gives details about the Checksum:

Data integrity
The statistics are grouped in blocks with a checksum appended. The last field in a block will always be "Checksum". The value is a single byte, and will not necessarily be a printable ASCII character. The modulo 256 sum of all bytes in a block will equal 0 if there were no transmission errors. Multiple blocks are sent containing different fields.

So, in the excerpt that you posted as output, you have two complete blocks, and an incomplete one, as the last block is missing the Checksum.
block = (b'\r\nH2\t0\r\n'
    b'H4\t0\r\n'
    b'H6\t-9001\r\n'
    b'H8\t28403\r\n'
    b'H10\t0\r\n'
    b'H12\t0\r\n'
    b'H18\t87\r\n'
    b'PID\t0x203\r\n'
    b'I\t0\r\n'
    b'CE\t0\r\n'
    b'TTG\t-1\r\n'
    b'Relay\tOFF\r\n'
    b'BMV\t700\r\n'
    b'Checksum\t\xd6')

Note that I have added the \r\n at the beginning, and removed them from the end of the block, such that the last byte is the block's checksum.
Now, we can calculate the checksum of the block:
>>> sum(block) % 256
213

This should have been zero. So, there may be some transmission problems, or they may be calculating the checksum differently from what they say in the docs.

Edit after new data.
Here is the code that I have used to inspect all the blocks that you have posted:
current_block = []
# it's unfortunate that lines start with `\r\n` rather than end with it
# we will read the first empty line separatey, and then include these
# 2 characters in the last line that is read
message = bmvdata.readline()
while True:
    # Note that we cannot decode yet, since the Checksum value may not be a valid utf8 character
    message = bmvdata.readline()

    # we save the message in the current block before any preprocessing
    current_block.append(message)

    #splitting on tabs (as bytes !!)
    message_parts = message.split(b'\t')

    if len(message_parts) > 1:
        key = message_parts[0].decode('utf8')  # here it is safe to decode the key
        value = message_parts[1].rstrip()  #stripping \r\n after the value

    if key == 'Checksum':
        block_chars = b''.join(current_block)
        checksum = sum(block_chars) % 256
        print('Retrieved  checksum', value[-1])
        print('Calculated checksum', checksum)
        print('----')

        # reset the block
        current_block = []

It is weird that for the first run you always get the calculated value equal retrieved + 1, but not in the second run. So, there may be some transmission problems.
Comment: Why are the checksum values different sometimes and does that matter when I am checking the bytes? Or do I just use the printed checksum value each time?
I have added additional outputs to the question to show what I mean.
Example: There are a few different checksum values that I have noticed. xd6, xd0, xd0, tT, xd8, xd7
Comment: @unmanned15 Thanks for uploading more data ! It seems that my supposition was completely false, so I removed it. I am adding a bit of code that I used to check different values. I don't know why it doesn't result in checksum zero, according to their definition
Answer: Below code compare two files in this case checksum as asker mentioned:
 bmvdata = batteryConnect(self.port,self.baudrate)
 bmvdata.flushInput()
 print(bmvdata.readline())

It has been done by the aid of hashlib
import hashlib
#File 1 = checksum
hasher1 = hashlib.md5()
#afile1 = open('checksum', 'rb')
buf1 = bmvdata.read()
#buf1 = afile1.read()
a = hasher1.update(buf1)
md5_a=(str(hasher1.hexdigest()))
#File 2
hasher2 = hashlib.md5()
afile2 = open('incoming-byte', 'rb')
buf2 = afile2.read()
b = hasher2.update(buf2)
md5_b=(str(hasher2.hexdigest()))
#Compare md5
if(md5_a==md5_b):
    print("Yes")
else:
    print("No")

Comment: Hi ! I suggest adding some text to you answer to detail how it solves the problem. Posting only the code with no explanation is not likely to help the asker, and, in this particular case, they have not mentioned that they are using any files . Thank you !
Comment: Hi, I thought it was very clean and clear but I added a description to it, asker asked checksum can be load from `bmvdata.readline()` and I hashed that buf1 and with another specific file.


Android - AddContentView defined in XML

Question: AdMob documentation to integrate it on Android instructs to create the following XML, if we want to create the view via XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:ads="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
  <com.google.android.gms.ads.AdView android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         ads:adUnitId="MY_AD_UNIT_ID"
                         ads:adSize="BANNER"/>
</LinearLayout>

And set it as content view to Activity:
public class BannerExample extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Look up the AdView as a resource and load a request.
    AdView adView = (AdView) this.findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);
  }
}

But in my case (I use cocos2d-x 3) I have already a subclass of Activity and I just need to add that view to my existing content, not set content view as it is done in the code above. How I can addContentView the view that is defined in XML?
Comment: could you please add code for your existing content view(xml file) and where you need to add this content in the existing content? Add some more explanation.
Answer: When you call.
setContentView(R.layout.main);

in onCreate it automatically inflates the view for you so you can start assigning your views from the xml to your own variables like such:
AdView adView = (AdView) findViewById(R.id.adView);

Make sure that the xml layout you defined at the top is the same that is being passed in setContentView(). In this case R.layout.main should contain a View with id adView of AdView type.
In other words, R.layout.main should be:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:ads="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
  <com.google.android.gms.ads.AdView android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         ads:adUnitId="MY_AD_UNIT_ID"
                         ads:adSize="BANNER"/>
</LinearLayout>

If what you want however, is inflate and use main layout and add the AdView, you must inflate the view yourself and add it to some element in main layout.
Like such:
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
item.addView(child);

References:
How to inflate one view with a layout
Comment: am I right that `setContentView` adds the View to the Activity to be seen when activity starts?
Comment: It adds the view if you are explicitly telling the activity to inflate the corresponding layout.
Answer: You can simply add
<com.google.android.gms.ads.AdView android:id="@+id/adView"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     ads:adUnitId="MY_AD_UNIT_ID"
                     ads:adSize="BANNER"/>

to your existing XML layout file.

Android - AddContentView defined in XML


"Cannot resolve symbol baoyz" Android Studio using baoyongzhang's SwipeMenuListView

Question: I've recently downloaded the github repo: https://github.com/baoyongzhang/SwipeMenuListView
Which allows to be a SwipeMenuListView. But the "baoyz" in the following import methods is red:
import com.boayz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuLayout;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.baoyz.swipemenulistview.SwipeMenuListView.OnMenuItemClickListener;
import com.baoyz.swipemenulistview.SwipeMenuListView.OnSwipeListener;

And all the Stuff I added (SwipeMenu,SwipeMenuCreator,SwipeMenuItem,SwipeMenuLayout,etc.) that are in the following code are also red:
 SwipeMenuCreator creator = new SwipeMenuCreator() {

    @Override
    public void create(SwipeMenu menu) {
        // create "open" item
        SwipeMenuItem openItem = new SwipeMenuItem(
                getApplicationContext());
        // set item background
        openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,
                0xCE)));
        // set item width
        openItem.setWidth(dp2px(90));
        // set item title
        openItem.setTitle("Open");
        // set item title fontsize
        openItem.setTitleSize(18);
        // set item title font color
        openItem.setTitleColor(Color.WHITE);
        // add to menu
        menu.addMenuItem(openItem);

        // create "delete" item
        SwipeMenuItem deleteItem = new SwipeMenuItem(
                getApplicationContext());
        // set item background
        deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
                0x3F, 0x25)));
        // set item width
        deleteItem.setWidth(dp2px(90));
        // set a icon
        deleteItem.setIcon(R.drawable.ic_delete);
        // add to menu
        menu.addMenuItem(deleteItem);
    }
};

Help appreciated! (Yes, I've downloaded the repo. But The only thing I did to it was extract it)
Comment: I'm using android studio
Comment: Did you just copy and paste the files?
Comment: @Zain which files? Explain to me what to do plz?
Comment: Are you just trying to run the sample repo code? Clean build, and see if that work.
Comment: @Zain no I am not trying to run the sample code. The samlple code gives me an error when trying to run it. So what I did was copy the code in the activity_main file and pasted it in a new activity, and added some stuff in the layout file.
Comment: You need to have reference to the import classes somewhere in your project, create a folder, but all the reference classes in there.
Answer: Create a package boayz - Copy + paste these files into this package.
Ensure each file builds and you have the correct imports etc.
Then ensure the project builds and hopefully it will run. It would have been easier for you to take the repo and see if you can run the samples.
Comment: Where do I create the package? Thanks for your interest in helping me; hope I'm not annoying you
Comment: In your src folder, where the other classes/packages are located. If you're new to Android look at Udacity Android course.
Comment: I tried running the samples but Android Studio gives me an error. #WhyIsItSoHard
Comment: @Zain  can u ppls help me how can i add loadMore items in listview onScrollListener using SwipeMenuListview i have to show user delete button on swipe everyitem of listview which is working fine now have to add loadMore items on scroll and pull to refresh listview
Comment: I created package com.baoyz.swipemenulistview and copied all files into package.
It works like a charm for me.