Q: Prevent triggering multiple triggers at same time on table update In the SQL server, I have a table X and a table Y.
The primary key of both is same say Z.
I created a trigger that if anything is created/deleted/updated in X then

*

*If the entry doesn't exist in Y for Z then create a new entry.

*If an entry exists in Y for Z then update that row.

I ran a query like this:
Delete FROM Table2 where TId = 1;

This query deleted 10 rows and 10 triggers ran simultaneously. As all ran parallel every trigger executed the else block because there was no entry in Y initially for Z and as all are running they won't find the row in Y. So because of this 10 rows gets created in the Y table.
I want that I should get only 1 entry created and the other triggers should update that entry.
As example I have tables:
Table1(TId PRIMARY KEY, C12, C13);
Table2(C21 PRIMARY KEY, TId FOREIGN KEY(Table1, C11), C23);
Table3(TId PRIMARY KEY, C32, C33);
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[trg1] ON  [dbo].[TABLE2] AFTER DELETE
        AS
        BEGIN
        SET NOCOUNT ON
            IF EXISTS (SELECT * FROM Deleted D )
                    BEGIN
                        IF EXISTS (Select * from [dbo].[Table3] where TId in (Select TId from Deleted D) )
                            BEGIN
                                Update [dbo].[Table3] SET C32 = 1 where TId in (Select TId from Deleted D);
                        END
                        ElSE
                            BEGIN
                            INSERT INTO [dbo].[Table3]
                                (
                                    TId,
                                    C32,
                                    C33
                                )
                            SELECT  TId,
                                    3,
                                    GETUTCDATE()
                            FROM Deleted D WHERE TId is not null
                            SET NOCOUNT OFF
                        END

            END
        SET NOCOUNT OFF
    END

PRINT '';
PRINT 'End of script';
PRINT ' --- // ---';

A: Your entire trigger's code should be something like this:
MERGE INTO Table3 t
USING (SELECT TId,CASE WHEN COUNT(*) > 1 THEN 1 ELSE 3 END as C32
       FROM deleted
       GROUP BY TId) u
ON t.TId = u.TId
WHEN MATCHED THEN UPDATE SET C32 = 1
WHEN NOT MATCHED AND u.TId is not null THEN
    INSERT (TId,C32,C33) VALUES (u.TId, u.C32, GETUTCDATE());

Which will insert a new row but decide whether to set C32 to 1 or 3, depending on how many rows in DELETED are for the same TId, and just update C32 to 1 if a row already existed.
This is all about thinking in sets. You weren't accounting for the fact that deleted could contain multiple rows, some or all of which may have the same TId value. You don't write IF/ELSE blocks that can only make a single decision for all rows in deleted1.

1E.g. if deleted had contained 4 rows total, 2 for TId 6 and 2 for TId 8, and Table3 had contained a row for TId 6 but no row for TId 8, your trigger would have found some matching rows in Table3 and just performed the UPDATE.


Q: android - how to find incompatible apis with older versions I have built an app with api 23/22/21 sdk with minTargetVersion = 16.
I use Android Studio
Sometimes during the development process, I saw android giving hints that a newer api is used when minTarget is 16.
I have fixed most of those but may not be all.
Now I have written lot of code and I am looking for a way to find out all usages of newer api which are not fully compatible with older versions.
I am looking for the same in layout files as well.
Is there an easy way I can find out?
Harder way is to look at every single line of code again or source files.

A: in Android Studio you can click on Analyze in the toolbar at the top > Inspect code > Whole Project
after AS is finished you will have a list of lint errors you can go through


Q: Gulp clean task being called from other tasks gulp.task('watch', gulp.series('clean', gulp.parallel('css')),
    gulp.watch(['./app/scss/**/*.scss'], gulp.series('clean', gulp.parallel('css')))
);

When using any other task in my gulpfile for some reason gulp.watch is being called. Does anyone know what is going wrong with this task?

A: gulp.task('watch', gulp.series('clean', gulp.parallel('css')), () => {
      gulp.watch(['./app/scss/**/*.scss'], gulp.series('clean', gulp.parallel('css')))
    }
);

It appears the issue is calling the gulp.watch function directly rather than from inside of a function. The above code fixes the issue.


Q: TwinCAT 3 user control Is it possible to create a library with Hmi usercontrols?
https://infosys.beckhoff.com/english.php?content=../content/1033/te2000_tc3_hmi_engineering/18014401986701963.html&id=
I have created a librarie for the plc with functionblocks and datastructs.
My usercontrols are mathed with my plcLib. And it would be nice if can use this set-up in every project

A: Your question is a bit unclear, however if you are asking about user access controls, whereby you can give certain users certain permissions, TwinCAT does have a built-in user management system. However, it relies on Beckhoff's HMI software products (Either HMI or HMI Web).
If you are using a different HMI solution, you would need to implement this functionality yourself.


Q: Optimizing pairwise subtruction I have to compute the pairwise difference of particle's velocities for around 1e6 objects each with approx. 1e4 particles. Right now I am using itertools.combinations to loop over the particles, but just for one object my code takes already more than 30 minutes. I was wondering what else can I do to speed it up to a feasible speed, since parallelising doesn't seem to add much in python. Is cython the way to go?
Here is my code just for one of the objects:
def pairwisevel(hist,velj,velk, xj, xk):
    vlos = (velj - velk)
    if( (xj - xk) < 0.):
        vlos = - vlos
    hist.add_from_value(vlos)

for i in itertools.combinations(np.arange(0,int(particles_per_group[0]),1),2):
    pairwisevel(hist, pvel[i[0]], pvel[i[1]],\
                pcoords[i[0]], pcoords[i[1]])

A: I hope I understood your qusition. In this example I calculated the histogramm of one particle object. But if you wan't to do this for all 1e6 groups (1e4*1e4*1e6=1e14) comparisons, this would still take a few days.
In this example I used Numba to accomplish the task.
Code
import numpy as np
import numba as nb
import time

#From Numba source
#Copyright (c) 2012, Anaconda, Inc.
#All rights reserved.

@nb.njit(fastmath=True)
def digitize(x, bins, right=False):
    # bins are monotonically-increasing
    n = len(bins)
    lo = 0
    hi = n

    if right:
        if np.isnan(x):
            # Find the first nan (i.e. the last from the end of bins,
            # since there shouldn't be many of them in practice)
            for i in range(n, 0, -1):
                if not np.isnan(bins[i - 1]):
                    return i
            return 0
        while hi > lo:
            mid = (lo + hi) >> 1
            if bins[mid] < x:
                # mid is too low => narrow to upper bins
                lo = mid + 1
            else:
                # mid is too high, or is a NaN => narrow to lower bins
                hi = mid
    else:
        if np.isnan(x):
            # NaNs end up in the last bin
            return n
        while hi > lo:
            mid = (lo + hi) >> 1
            if bins[mid] <= x:
                # mid is too low => narrow to upper bins
                lo = mid + 1
            else:
                # mid is too high, or is a NaN => narrow to lower bins
                hi = mid

    return lo

#Variant_1
@nb.njit(fastmath=True,parallel=True)
def bincount_comb_1(pvel,pcoords,bins):
  vlos_binned=np.zeros(bins.shape[0]+1,dtype=np.uint64)
  for i in nb.prange(pvel.shape[0]):
    for j in range(pvel.shape[0]):
      if( (pcoords[i] - pcoords[j]) < 0.):
        vlos = 0.
      else:
        vlos = (pvel[i] - pvel[j])

      dig_vlos=digitize(vlos, bins, right=False)
      vlos_binned[dig_vlos]+=1
  return vlos_binned

#Variant_2
#Is this also working?
@nb.njit(fastmath=True,parallel=True)
def bincount_comb_2(pvel,pcoords,bins):
  vlos_binned=np.zeros(bins.shape[0]+1,dtype=np.uint64)
  for i in nb.prange(pvel.shape[0]):
    for j in range(pvel.shape[0]):
      #only particles which fulfill this condition are counted?
      if( (pcoords[i] - pcoords[j]) < 0.):
        vlos = (pvel[i] - pvel[j])
        dig_vlos=digitize(vlos, bins, right=False)
        vlos_binned[dig_vlos]+=1
  return vlos_binned

#Variant_3
#Only counting once
@nb.njit(fastmath=True,parallel=True)
def bincount_comb_3(pvel,pcoords,bins):
  vlos_binned=np.zeros(bins.shape[0]+1,dtype=np.uint64)
  for i in nb.prange(pvel.shape[0]):
    for j in range(i,pvel.shape[0]):
      #only particles, where this condition is met are counted?
      if( (pcoords[i] - pcoords[j]) < 0.):
        vlos = (pvel[i] - pvel[j])
        dig_vlos=digitize(vlos, bins, right=False)
        vlos_binned[dig_vlos]+=1
  return vlos_binned

#Create some data to test
bins=np.arange(2,32)
pvel=np.random.rand(10_000)*35
pcoords=np.random.rand(10_000)*35

#first call has compilation overhead, we don't measure this
res_1=bincount_comb_1(pvel,pcoords,bins)
res_2=bincount_comb_2(pvel,pcoords,bins)

t1=time.time()
res=bincount_comb_1(pvel,pcoords,bins)
print(time.time()-t1)
t1=time.time()
res=bincount_comb_2(pvel,pcoords,bins)
print(time.time()-t1)
t1=time.time()
res=bincount_comb_3(pvel,pcoords,bins)
print(time.time()-t1)

Performance
#Variant_1: 0.5s 5.78d for 1e6 groups of points
#Variant_2: 0.3s 3.25d for 1e6 groups of points
#Variant_3: 0.22s 2.54d for 1e6 groups of points


Q: Matlab, finding common values in two array I want to find common values in two arrays(For example if A=[1 2 3 4 5 6] and B=[9 8 7 6 3 1 2] the result is ans=[1 2 3 6]).Is there any method without using loop?
Thanks

A: use intersect(A,B) to get the answer.
Another option is to use ismember, for example A(ismember(A,B)).


Q: Possible to save and list queries in MySQL? I have a database and have saved the code on a separate document for a number of queries - is there anyway of saving them as part of the database as some kind of list so that they can be clicked on to run them rather than keeping on having to paste the code in?
I realise that there are various Reports systems available to purchase on line, but this is a small database and a one-off, and they look complicated and not worth the trouble.
Is there anyway of streamlining this?

A: If I understand correctly you can write those sql queries to a script-or bunch of script files- file and directly run on mysql without copy/paste.
 mysql -u user -ppass < script.sql


Q: Generating repeated iterations of data across two data frames in R I'm trying to calculate distance between two sets of phonetic variables in R using an established matrix of phonetic measurements. For example, I want to take the measurements for /p/ and the measurements for /b/, and then compare the distance between these two phonetic units. I can do this using the following matrix of distinctive phonetic features:
 library(tibble)
 distinctive.feature.matrix <- tribble(~Symbol, ~Sonorant, ~Consonantal, ~Voice, ~Nasal,
 ~Degree, ~Labial, ~Palatal, ~Pharyngeal, ~Round, ~Tongue, ~Radical,
                                       "p", -1, 1, -1, -1, 1, 1, 0, -1, 1, 0, 0,
                                       "b", -1, 1, 0, -1, 1, 1, 0, -1, 1, 0, 0,
                                       "t", -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 0,
                                       "d", -1, 1, 0, -1, 1, -1, 1, -1, -1, 1, 0,
                                       "k", -1, 1, -1, -1, 1, -1, -1, -1, -1, -1, 0,
                                       "g", -1, 1, 0, -1, 1, -1, -1, -1, -1, -1, 0,
                                       "f", -0.5, 1, -1, -1, 0, -1, 1, -1, 1, 0, 0,
                                       "v", -0.5, 1, 0, -1, 0, -1, 1, -1, 1, 0, 0,
                                       "θ", -0.5, 1, -1, -1, 0, -1, 1, -1, -1, 0, 0,
                                       "ð", -0.5, 1, 0, -1, 0, -1, 1, -1, -1, 0, 0,
                                       "s", -0.5, 1, -1, -1, 0, -1, 1, -1, -1, 1, 0,
                                       "z", -0.5, 1, 0, -1, 0, -1, 1, -1, -1, 1, 0,
                                       "h", -0.5, 1, 0, -1, 0, -1, -1, 1, -1, -1, -1,
                                       "ʃ", -0.5, 1, -1, -1, 0, -1, 0, -1, -1, 0, 0,
                                       "ʒ", -0.5, 1, 0, -1, 0, -1, 0, -1, -1, 0, 0,
                                       "tʃ", -0.8, 1, -1, -1, 1, -1, 0, -1, -1, 0, 0,
                                       "dʒ", -0.8, 1, 0, -1, 1, -1, 0, -1, -1, 0, 0,
                                       "m", 0, 0, 1, 1, 1, 1, 0, -1, 1, 0, 0,
                                       "n", 0, 0, 1, 1, 1, -1, 1, -1, -1, 1, 0,
                                       "ŋ", 0, 0, 1, 1, 1, -1, -1, -1, -1, -1, 0,
                                       "r", 0.5, 0, 1, 0, -1, -1, -1, 1, 1, -1, -1,
                                       "l", 0.5, 0, 1, 0, -1, -1, 1, -1, -1, 1, 0,
                                       "w", 0.8, 0, 1, 0, 0, 1, -1, -1, 1, -1, 0,
                                       "j", 0.8, 0, 1, 0, 0, -1, 0, -1, -1, 0, 1)

I have another set of data showing child productions of different words, and I want to calculate the distance between the child's production and that of the target, for each consonant in the target word.
The production data looks a bit like this:
 library(tibble)
 production.data <- tribble(~Subject, ~Age, ~Target, ~C1_target, ~C1_actual,
      "subj1", "001126", "teddy", "t", "d",
      "subj1", "001126", "teddy", "t", "t",
      "subj1", "001126", "daddy", "d", "d",
      "subj1", "001126", "daddy", "d", "d",
      "subj1", "001126", "daddy", "d", "t",
      "subj1", "001126", "baby", "b", "p",
      "subj1", "001126", "Tigger", "t", "d",
      "subj1", "001126", "doggy", "d", "d",
      "subj1", "001126", "milk", "m", "m")

So, for each instance of production.data$C1_target I want to take the values of the corresponding consonant in distinctive.feature.matrix and compare them with the values of productiondata$C1_actual. Once I have these values, I will subtract the C1_actual value from the C1_target value, across each of the 11 distinctive features.
By way of example, for the first instance of 'teddy', I want to compare /t/ with /d/, which means subtracting -1 from -1 (distinctive.feature.matrix$Sonorant), 1 from 1 (distinctive.feature.matrix$Consonantal), 0 from -1 (distinctive.feature.matrix$Voice), etc. I'll then do some further calculations on these, but I'll leave it as this for now as it's already quite complicated.
I think I can use for loops to do this but I haven't used this function before and no amount of searching brings up a usable example.

A: The dplyr package provides join functions that can help.
For every row of the production.data, you can bring in the corresponding features for each C1_target, C1_actual to create a large tibble:
library(dplyr)
x <- production.data %>%
     inner_join(distinctive.feature.matrix, by = c("C1_target"="Symbol")) %>%
     inner_join(distinctive.feature.matrix, by = c("C1_actual"="Symbol"))

Note that there are two calls to inner_join: one to get features corresponding to C1_target, the second for features for C1_actual
The new tibble has column names such as Sonorant.x and Sonorant.y, the first corresponding to C1_target and the second corresponding to C1_actual.
You can create a list of feature names by taking the column names, excluding Symbol:
features <- colnames(distinctive.feature.matrix)[-1]

Now you can do your 'for-loop' to calculate the difference between the x and y values, and then combine the lot into a new data frame:
diffs <- do.call(cbind, sapply(features, function(f) x[paste0(f, '.x')] - x[paste0(f, '.y')]))
colnames(diffs) <- features

The paste0 function concatenates each feature name with .x or .y; the sapply is the equivalent of the for-loop; and the cbind bashes the result of each computation into a new table.
You will end up with a data frame whose column names are the features, and as many rows as your production.data had.
To be fair, the above code is an ugly confection of dplyr-like syntax and base R...


Q: System.ArgumentOutOfRangeException' occurred in mscorlib.dll but was not handled in user code Index was out of range Getting:

"An exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll but was not handled in user code
      Index was out of range. Must be non-negative and less than the size of the collection."

in
if (resD.Tables.Contains(ReDo.TableName))
{
    _result = resD.Tables[ReDo.TableName];
}

When I try to execute this code repeatedly, I am getting above exception, though, resD.Table contains the table name specified in condition. Any help much appreciated.


Q: Android SDK problem setting ringtone I am trying to add and set the default ringtone on the emulator/phone. The ringtone has been downloaded and is stored in the application folder /dada/dada/com.xxx/ringtones. the ringtones are ogg files.
I use the following code to add and set the ringtone:
public void setRingtone() {
    aajoAsset asset = null;
    asset = mXXXX.getAssetManager().getCurrentRingtoneAsset();

    if (asset != null && asset.isSaved()/* && !asset.getName().equals(mLastAssetName)*/) {
        String filepath = asset.getDirectoryPath() + asset.getFilename();
        File ringtoneFile = new File(filepath);

        if (LOG) {
            Log.i(TAG, "Sending Intent : " + Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        }

        mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + asset.getDirectoryPath() + asset.getFilename())));

        ContentValues content = new ContentValues();
        content.put(MediaStore.MediaColumns.DATA,ringtoneFile.getAbsolutePath());
        content.put(MediaStore.MediaColumns.TITLE, "1234");
        content.put(MediaStore.MediaColumns.SIZE, asset.getSize());
        content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
        content.put(MediaStore.Audio.Media.ARTIST, "1234");
        content.put(MediaStore.Audio.Media.DURATION, 4800);
        content.put(MediaStore.Audio.Media.IS_RINGTONE, true);
        content.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
        content.put(MediaStore.Audio.Media.IS_ALARM, false);
        content.put(MediaStore.Audio.Media.IS_MUSIC, false);

        if (LOG) {
            Log.i(TAG, "the absolute path of the file is : " + ringtoneFile.getAbsolutePath());
        }

        Uri uri = MediaStore.Audio.Media.getContentUriForPath(
        ringtoneFile.getAbsolutePath());
        Uri newUri = mContext.getContentResolver().insert(uri, content);

        if (LOG) {
            Log.i(TAG,"the ringtone uri is : " + newUri);
        }

        RingtoneManager.setActualDefaultRingtoneUri(mContext, RingtoneManager.TYPE_RINGTONE, newUri);

        mLastAssetName = asset.getName();
    }

The code executes fine and the ringtone shows in the ringtone list but when I click on it to test it or when I simulate a incoming call I get the following errors in LogCat:
DEBUG/MediaPlayer(1230): Couldn't open file on client side, trying server side
ERROR/MediaPlayerService(33): Couldn't open fd for content://media/internal/audio/media/1
ERROR/MediaPlayer(1230): Unable to to create media player
ERROR/RingtoneManager(1230): Failed to open ringtone content://media/internal/audio/media/1

I have searched for solutions in many forums and cannot find any.
any help is most welcome
Thanks


Q: How to turn column of number into a list of strings? I don't know why I cant figure this out. But I have a column of numbers that I would like to turn into a list of strings. I should of mention this when i initially posted this but this isn't a DataFrame or did it come from a file this is a result of a some code, sorry wasn't trying to waste anybody's time, I just didn't want to add a bunch of clutter. This is exactly how it prints out.
Here is my column of numbers.
3,1,3
3,1,3
3,1,3
3,3,3
3,1,1

And I would like them to look like this.
['3,1,3', '3,1,3', '3,1,3', '3,3,3', '3,1,1']

I'm trying to find a way that is not dependent on how many numbers are in each row or how many sets of numbers are in the column.
Thanks, really appreciate it.

A: Assume you start with a DataFrame
df = pd.DataFrame([[3, 1, 3], [3, 1, 3], [3, 1, 3], [3, 3, 3], [3, 1, 1]])

df.astype(str).apply(lambda x: ','.join(x.values), axis=1).values.tolist()

Looks like:
['3,1,3', '3,1,3', '3,1,3', '3,3,3', '3,1,1']

A: def foo():
    l = []
    with open("file.asd", "r") as f:
        for line in f:
            l.append(line)
    return l

A: To turn your dataframe in to strings, use the astype function:
df = pd.DataFrame([[3, 1, 3], [3, 1, 3], [3, 1, 3], [3, 3, 3], [3, 1, 1]])
df = df.astype('str')

Then manipulating your columns becomes easy, you can for instance create a new column:
In [29]:

df['temp'] = df[0] + ',' + df[1] + ',' + df[2]
df
Out[29]:
0   1   2   temp
0   3   1   3   3,1,3
1   3   1   3   3,1,3
2   3   1   3   3,1,3
3   3   3   3   3,3,3
4   3   1   1   3,1,1

And then compact it into a list:
In [30]:

list(df['temp'])
Out[30]:
['3,1,3', '3,1,3', '3,1,3', '3,3,3', '3,1,1']

A: # Done in Jupyter notebook
# add three quotes on each side of your column.
# The advantage to dataframe is the minimal number of operations for
# reformatting your column of numbers or column of text strings into
# a single string

a = """3,1,3
3,1,3
3,1,3
3,3,3
3,1,1"""

b = f'"{a}"'
print('String created with triple quotes:')
print(b)

c = a.split('\n')
print ("Use split() function on the string. Split on newline character:")
print(c)

print ("Use splitlines() function on the string:")
print(a.splitlines())


Q: How to compare text strings in a table colum matlab If I have an N-by-1 table colum how is it possible to detect if any of the rows is identical?

A: If you simply want to determine if there are duplicate rows, you can use unique to do this. You can check the number of unique values in the column and compare this to the total number of elements (numel) in the same column
tf = unique(t.Column) == numel(t.Column)

If you want to determine which rows are duplicates, you can again use unique but use the third output and then use accumarray to count the number of occurrences of each value and then select those values which appear more than once.
[vals, ~, inds] = unique(t.Column, 'stable');
repeats = vals(accumarray(inds, 1) > 1);

% And to print them out:
fprintf('Duplicate value: %s\n', repeats{:})

If you want a logical vector of true/false for where the duplicates exist you can do something similar to that above
[vals, ~, inds] = unique(t.Column, 'stable');
result = ismember(inds, find(accumarray(inds, 1) > 1));

Or
[vals, ~, inds] = unique(t.Column, 'stable');
result = sum(bsxfun(@eq, inds, inds.'), 2) > 1;

Update
You can combine the two approaches above to accomplish what you want.
[vals, ~, inds] = unique(t.Column, 'stable');
repeats = vals(accumarray(inds, 1) > 1);

hasDupes = numel(repeats) > 0;

if hasDupes
    for k = 1:numel(repeats)
        fprintf('Duplicate value: %s\n', repeats{k});
        fprintf('   Found at: ');
        fprintf('%d ', find(strcmp(repeats{k}, t.Column)));
        fprintf('\n');
    end
end


Q: Find newest message from each sender? I've got a Messages menu in my app, and I'm completely at a loss on how to show the newest message received/sent from EACH user, without loading ALL messages received/sent.
I'm using Parse as my backend, so I've gotta believe there's some clever subquery, or NSPredicate that I could user for this.
The current solution I have in mind is another Boolean attribute for my messages, where all but the most recent get augmented, so I can known what to load with a simple predicate. This seems slopy though, save me from myself!
EDIT:
Right now I'm getting the messages where the user is the sender or receiver (depending on a segmented control), and then displaying each user who was sent to/received from with a sample of their newest message.
Right now I'm using internal logic to do this, and still in the process of figuring it out (the logic is a little backwards right now...)
let query = PFQuery(className: "Messages")

            if recievedOrSent {
            query.whereKey("other", equalTo: userName)
            print("called OTHER query")
        } else {
            query.whereKey("sender", equalTo: userName)
            print("called SENDER query")
        }

        query.addDescendingOrder("createdAt")
        query.findObjectsInBackgroundWithBlock {
            (objects, error) -> Void in

            if error == nil {

                    if thisSender == userName || self.recievedUsers.contains(thisSender) &&  self.lesserDate(thisTime, rhs: self.recievedUsersTimes[self.recievedUsers.indexOf(thisSender)!])
                    {} else {
                        self.recievedUsers.append(thisSender)
                        self.recievedUsersMsg.append(thisMessage)
                        self.recievedUsersTimes.append(thisTime)
                        self.recievedUsersMsgRead.append(thisRead)
                    }

                    if thisOther == userName || self.sentUsers.contains(thisOther) && self.lesserDate(thisTime, rhs: self.sentUsersTimes[self.sentUsers.indexOf(thisOther)!])
                    {} else {
                        self.sentUsers.append(thisOther)
                        self.sentUsersMsg.append(thisMessage)
                        self.sentUsersTimes.append(thisTime)
                        self.sentUsersMsgRead.append(thisRead)
                }
    }

A: If you want to get only the most recent message from each user that you are having a conversation with, create a new class called RecentMessage and update it each time using an afterSave cloud function on the Message class.
In the afterSave hook, maintain a pointer in the RecentMessage class to the latest Message in the conversation for each user. Then all you have to do is query for all of the current user's RecentMessage objects and use includeKey on the Message pointer.
This let's you abstract away more logic from the client side and streamline your queries where performance really counts :)


Q: Get the alternate rows in SQL I have a table PERSON with a single column GENDER, and values in 6 rows is like:
GENDER

M
M
M
F
F
F

The output should be like
GENDER

M
F
M
F
M
F

What should be the SQL query to get such output? I believe ROWNUMBER() must be used.

A: SELECT GENDER, R = ROW_NUMBER() OVER (PARTITION BY GENDER ORDER BY GENDER)
FROM   PERSON
ORDER BY R, GENDER DESC


Q: Unity Photon PUN - unable to get IPunObservable call back to operate I am trying to understand the Photon Pun architecture. I cannot get the callback OnPhotonSerializeView() in 'IPunObservable' to be called.
What I have in my scene is

*

*a GameObject called DataHandler at the top of the hierachy

*DataHandler has a PhotonView and PhotonTransformView attached

*DataHandler also has a script attached called DataHandler

*The DataHandler script component has been dragged to the first observable in the PhotonView component (so it shows 'DataHandler (DataHandler)' as the observed component).

*The DataHandler script implements IPunObservable, and the one method in that is
 public class DataHandler : MonoBehaviourPunCallbacks, IPunObservable
 {
 ....

     public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
     {
         Debug.Log("OnPhotonSerializeView(): " + (stream.IsWriting ? "writing" : "reading"));
     }
 ....
 }

Nothing shows up in the log when it is run.
However when built and run as a Windows EXE, connected with a room in place, it comes up with NullReferenceExceptions. The log shows:
    NullReferenceException: Object reference not set to an instance of an object
      at Photon.Pun.PhotonView.SerializeComponent (UnityEngine.Component component, Photon.Pun.PhotonStream stream, Photon.Pun.PhotonMessageInfo info) [0x0001e] in C:\Users\nick\Documents\Unity3D\PhotonPunTest\Assets\Photon\PhotonUnityNetworking\Code\PhotonView.cs:368
      at Photon.Pun.PhotonView.SerializeView (Photon.Pun.PhotonStream stream, Photon.Pun.PhotonMessageInfo info) [0x00024] in C:\Users\nick\Documents\Unity3D\PhotonPunTest\Assets\Photon\PhotonUnityNetworking\Code\PhotonView.cs:330
      at Photon.Pun.PhotonNetwork.OnSerializeWrite (Photon.Pun.PhotonView view) [0x00089] in C:\Users\nick\Documents\Unity3D\PhotonPunTest\Assets\Photon\PhotonUnityNetworking\Code\PhotonNetworkPart.cs:1593
      at Photon.Pun.PhotonNetwork.RunViewUpdate () [0x000b3] in C:\Users\nick\Documents\Unity3D\PhotonPunTest\Assets\Photon\PhotonUnityNetworking\Code\PhotonNetworkPart.cs:1522
      at Photon.Pun.PhotonHandler.LateUpdate () [0x00042] in C:\Users\nick\Documents\Unity3D\PhotonPunTest\Assets\Photon\PhotonUnityNetworking\Code\PhotonHandler.cs:155

    (Filename: C:/Users/nick/Documents/Unity3D/PhotonPunTest/Assets/Photon/PhotonUnityNetworking/Code/PhotonView.cs Line: 368)

and the code around line 368 in PhotonView.cs is
    protected internal void SerializeComponent(Component component, PhotonStream stream, PhotonMessageInfo info)
    {
        IPunObservable observable = component as IPunObservable;
        if (observable != null)
        {
            observable.OnPhotonSerializeView(stream, info);
        }
        else
        {
            Debug.LogError("Observed scripts have to implement IPunObservable. "+ component + " does not. It is Type: " + component.GetType(), component.gameObject);
        }
    }

(The Debug.LogError line is 368).
Don't see what is wrong, though I assume component is probably null. The script is implementing IPunObservable. Can anyone help?

A: The problem here is the photon logic about local player and master client. The rules about who can send are bound up in this.
In the end I found it much easier to go below the higher level 'helper' components (PhotonView etc) and send everything with the lowever level RaiseEvent() / OnEvent() mechanism.
See answer to previous question.


Q: Django: Cannot assign "'2'": "Issues.reporter" must be a "User" instance I am working on a simple "issue tracking" web application as way to learn more about Django.
I am using Django 4.1.4 and Python 3.9.2.
I have the following classes in models.py (which may look familiar to people familiar with JIRA):

*

*Components

*Issues

*IssueStates

*IssueTypes

*Priorities

*Projects

*Releases

*Sprints

Originally I also had a Users class in models.py but now am trying to switch to using the Django User model. (The User class no longer exists in my models.py)
I have been studying the following pages to learn how best to migrate to using the Django Users model.

*

*Django Best Practices: Referencing the User Model

*Referencing the User Model
All of my List/Detail/Create/Delete view classes worked fine with all of the above models until I started working on using the Django User class.
-- models.py --

from django.conf import settings

class Issues(models.Model):

    id = models.BigAutoField(primary_key=True)
    project = models.ForeignKey(
        to=Projects, on_delete=models.RESTRICT, blank=True, null=True
    )
    summary = models.CharField(max_length=80, blank=False, null=False, default="")
    issue_type = models.ForeignKey(
        to=IssueTypes, on_delete=models.RESTRICT, blank=True, null=True
    )
    issue_state = models.ForeignKey(
        to=IssueStates, on_delete=models.RESTRICT, blank=True, null=True, default="New"
    )

    # https://learndjango.com/tutorials/django-best-practices-referencing-user-model
    # https://docs.djangoproject.com/en/4.0/topics/auth/customizing/#referencing-the-user-model
    reporter = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.RESTRICT,
        related_name="reporter_id",
    )
    priority = models.ForeignKey(
        to=Priorities, on_delete=models.RESTRICT, blank=True, null=True
    )
    component = models.ForeignKey(
        to=Components, on_delete=models.RESTRICT, blank=True, null=True
    )
    description = models.TextField(blank=True, null=True)
    planned_release = models.ForeignKey(
        to=Releases, on_delete=models.RESTRICT, blank=True, null=True
    )

    # https://learndjango.com/tutorials/django-best-practices-referencing-user-model
    # https://docs.djangoproject.com/en/4.0/topics/auth/customizing/#referencing-the-user-model
    assignee = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.RESTRICT,
        related_name="assignee_id",
    )

    slug = models.ForeignKey(
        to="IssueSlugs", on_delete=models.RESTRICT, blank=True, null=True
    )

    sprint = models.ForeignKey(
        to=Sprints, on_delete=models.RESTRICT, blank=True, null=True
    )

    def save(self, *args, **kwargs):

        if not self.slug:

            # generate slug for this new Issue
            slug = IssueSlugs()
            slug.project_id = self.project.id
            slug.save()

            self.slug = slug

        super().save(*args, **kwargs)

    def __str__(self):
        return self.slug.__str__() + " - " + self.summary.__str__()

    class Meta:
        managed = True
        db_table = "issues"

class IssueSlugs(models.Model):
    """
    This table is used to generate unique identifiers for records in the
    Issues table. My goal was to model the default behavior found in JIRA
    where each Issue has a unique identifier that is a combination of:

    1) the project abbreviation
    2) a sequential number for the project

    So here when creating a new Issue record, if it is the first record for
    a particular project, the sequential number starts at 100, otherwise it
    is the next sequential number for the project.
    """

    id = models.BigAutoField(primary_key=True)

    project = models.ForeignKey(
        to=Projects, on_delete=models.RESTRICT, blank=True, null=True
    )

    slug_id = models.IntegerField(default=100)

    slug = models.CharField(
        max_length=80,
        blank=False,
        null=False,
        unique=True,
    )

    def __str__(self):
        return self.slug.__str__()

    def save(self, *args, **kwargs):

        if not self.slug:

            result = IssueSlugs.objects.filter(
                project_id__exact=self.project.id
            ).aggregate(Max("slug_id"))

            # The first issue being created for the project
            #   {'slug_id__max': None}
            if not result["slug_id__max"]:
                self.slug_id = 100
                self.slug = self.project.abbreviation + "-" + str(100)

            else:
                logging.debug(result)

                next_slug_id = result["slug_id__max"] + 1

                self.slug_id = next_slug_id
                self.slug = self.project.abbreviation + "-" + str(next_slug_id)

        super().save(*args, **kwargs)

    class Meta:
        managed = True
        db_table = "issue_slugs"

-- issues.py --

class CreateUpdateIssueForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # save for IssueCreateView.form_valid()
        self.kwargs = kwargs

        font_size = "12pt"
        for field_name in self.fields:
            if field_name in ("summary", "description"):
                self.fields[field_name].widget.attrs.update(
                    {
                        "size": self.fields[field_name].max_length,
                        "style": "font-size: {0}".format(font_size),
                    }
                )
            elif field_name in ("reporter", "assignee"):

                # https://docs.djangoproject.com/en/4.0/topics/auth/customizing/#referencing-the-user-model
                User = get_user_model()

                choices = list()
                choices.append(("", ""))

                for element in [
                    {
                        "id": getattr(row, "id"),
                        "display": row.get_full_name(),
                    }
                    for row in User.objects.exclude(is_superuser__exact="t")
                ]:
                    choices.append((element["id"], element["display"]))

                self.fields[field_name] = forms.fields.ChoiceField(
                    choices=choices,
                    # I had to specify required=False here to eliminate a very
                    # strange error:
                    # An invalid form control with name='assignee' is not focusable.
                    required=False,
                )

            else:
                # all the <select> fields ...
                self.fields[field_name].widget.attrs.update(
                    {
                        "class": ".my-select",
                    }
                )

    class Meta:
        model = Issues
        fields = [
            "project",
            "summary",
            "component",
            "description",
            "issue_type",
            "issue_state",
            "reporter",
            "priority",
            "planned_release",
            "assignee",
            "sprint",
        ]

class IssueCreateView(LoginRequiredMixin, PermissionRequiredMixin, generic.CreateView):
    """
    A view that displays a form for creating an object, redisplaying the form
    with validation errors (if there are any) and saving the object.

    https://docs.djangoproject.com/en/4.1/ref/class-based-views/generic-editing/#createview
    """

    model = Issues
    permission_required = "ui.add_{0}".format(model.__name__.lower())
    template_name = "ui/issues/issue_create.html"
    success_url = "/ui/issue_list"
    form_class = CreateUpdateIssueForm

    def form_valid(self, form):

        User = get_user_model()
        if "reporter" in self.kwargs:
            form.instance.reporter = User.objects.get(id__exact=self.kwargs["reporter"])

        if not form.is_valid():
            messages.add_message(
                self.request, messages.ERROR, "ERROR: '{0}'.".format(form.errors)
            )

            return super().form_valid(form)

        action = self.request.POST["action"]

        if action == "Cancel":
            # https://docs.djangoproject.com/en/4.1/topics/http/shortcuts/#django.shortcuts.redirect
            return redirect("/ui/issue_list")

        return super().form_valid(form)

    def get_initial(self):
        """
        When creating a new Issue I'm setting default values for a few
        fields on the Create Issue page.
        """
        # https://docs.djangoproject.com/en/4.0/topics/auth/customizing/#referencing-the-user-model
        User = get_user_model()

        from ui.models import IssueStates, Priorities, IssueTypes

        issue_state = IssueStates.objects.get(state__exact="New")
        priority = Priorities.objects.get(priority__exact="Medium")
        issue_type = IssueTypes.objects.get(issue_type__exact="Task")
        reporter = User.objects.get(username__exact=self.request.user)

        return {
            "issue_state": issue_state.id,
            "priority": priority.id,
            "issue_type": issue_type.id,
            "reporter": reporter.id,
        }

When I try to create a new Issue, the "new Issue" form is displayed normally, but when I save the form I get a Django error with a stack trace I don't understand because it does not have a reference to any of my code, so I have no idea where to start debugging.
16:22:48 ERROR    Internal Server Error: /ui/issue/create
Traceback (most recent call last):
  File "/Users/a0r470/git/issue_tracker/env/lib/python3.9/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/Users/a0r470/git/issue_tracker/env/lib/python3.9/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/a0r470/git/issue_tracker/env/lib/python3.9/site-packages/django/views/generic/base.py", line 103, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/a0r470/git/issue_tracker/env/lib/python3.9/site-packages/django/contrib/auth/mixins.py", line 73, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/Users/a0r470/git/issue_tracker/env/lib/python3.9/site-packages/django/contrib/auth/mixins.py", line 109, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/Users/a0r470/git/issue_tracker/env/lib/python3.9/site-packages/django/views/generic/base.py", line 142, in dispatch
    return handler(request, *args, **kwargs)
  File "/Users/a0r470/git/issue_tracker/env/lib/python3.9/site-packages/django/views/generic/edit.py", line 184, in post
    return super().post(request, *args, **kwargs)
  File "/Users/a0r470/git/issue_tracker/env/lib/python3.9/site-packages/django/views/generic/edit.py", line 152, in post
    if form.is_valid():
  File "/Users/a0r470/git/issue_tracker/env/lib/python3.9/site-packages/django/forms/forms.py", line 205, in is_valid
    return self.is_bound and not self.errors
  File "/Users/a0r470/git/issue_tracker/env/lib/python3.9/site-packages/django/forms/forms.py", line 200, in errors
    self.full_clean()
  File "/Users/a0r470/git/issue_tracker/env/lib/python3.9/site-packages/django/forms/forms.py", line 439, in full_clean
    self._post_clean()
  File "/Users/a0r470/git/issue_tracker/env/lib/python3.9/site-packages/django/forms/models.py", line 485, in _post_clean
    self.instance = construct_instance(
  File "/Users/a0r470/git/issue_tracker/env/lib/python3.9/site-packages/django/forms/models.py", line 82, in construct_instance
    f.save_form_data(instance, cleaned_data[f.name])
  File "/Users/a0r470/git/issue_tracker/env/lib/python3.9/site-packages/django/db/models/fields/__init__.py", line 1006, in save_form_data
    setattr(instance, self.name, data)
  File "/Users/a0r470/git/issue_tracker/env/lib/python3.9/site-packages/django/db/models/fields/related_descriptors.py", line 237, in __set__
    raise ValueError(
ValueError: Cannot assign "'2'": "Issues.reporter" must be a "User" instance.
[27/Dec/2022 16:22:48] "POST /ui/issue/create HTTP/1.1" 500 120153

Generally I understand that under the covers, Django creates two fields in the Issues model for me:

*

*reporter

*reporter_id

and I understand that the reporter field needs to contain a User instance instead of an integer (2). BUT I don't know WHERE in my code I should do this assignment.
I have tried overriding a few methods in my CreateUpdateIssueForm and IssueCreateView as a way to try to find where my code is causing problems - no luck so far.
In my IssueCreateView(generic.CreateView) class, I added the following to my form_valid() method, intending to retrieve the correct User record and assign it to form.instance.reporter, but the code appears to be failing before it gets to my form_valid() method.
    def form_valid(self, form):

        User = get_user_model()
        if "reporter" in self.kwargs:
            form.instance.reporter = User.objects.get(id__exact=self.kwargs["reporter"])

Clearly I do not fully understand the flow of control in these Generic View classes.
Thank you for any help you can provide!

A: I discovered that trying to migrate my own Users model to a CustomUser model is a non-trivial undertaking! I learned this from Will Vincent and his excellent post on this very topic!
Django Best Practices: Custom User Model
The Django documentation also states that migrating to the Django User in the midst of an existing project is non-trivial.
Changing to a custom user model mid-project
So, to solve my problem I started with a new empty project with only the CustomUser in my models.py as Mr. Vincent described, which worked perfectly.
After that, I setup the rest of my model classes in models.py, referencing the CustomUser model as needed.
assignee = models.ForeignKey(
    to=CustomUser,
    on_delete=models.RESTRICT,
    blank=True,
    null=True,
)

And copied the rest of my template files, view source files, static files, etc. from my original project into this new project.
My codebase is now working as expected using the Django User model.
Huge Thanks to Mr. Will Vincent's excellent article on this issue!


Q: Where to draw the line - is it possible to love LINQ too much? I recently found LINQ and love it.  I find lots of occasions where use of it is so much more expressive than the longhand version but a colleague passed a comment about me abusing this technology which now has me second guessing myself.  It is my perspective that if a technology works efficiently and the code is elegant then why not use it?  Is that wrong?  I could spend extra time writing out processes "longhand" and while the resulting code may be a few ms faster, it's 2-3 times more code and therefore 2-3 times more chance that there may be bugs.
Is my view wrong?  Should I be writing my code out longhand rather than using LINQ?  Isn't this what LINQ was designed for?
Edit: I was speaking about LINQ to objects, I don't use LINQ to XML so much and I have used LINQ to SQL but I'm not so enamoured with those flavours as LINQ to objects.

A: Yes you can love LINQ too much - Single Statement LINQ RayTracer
Where do you draw the line? I'd say use LINQ as much as it makes the code simpler and easier to read.
The moment the LINQ version becomes more difficult to understand then the non-LINQ version it's time to swap, and vice versa. EDIT: This mainly applies to LINQ-To-Objects as the other LINQ flavours have their own benefits.

A: Its not possible to love Linq to Objects too much, it's a freaking awesome technology !
But seriously, anything that makes your code simple to read, simple to maintain and does the job it was intended for, then you would be silly not to use it as much as you can.

A: LINQ's supposed to be used to make filtering, sorting, aggregating and manipulating data from various sources as intuitive and expressive as possible. I'd say, use it wherever you feel it's the tidiest, most expressive and most natural syntax for doing what it is you're trying to do, and don't feel guilty about it.
If you start humping the documentation, then it may be time to reconsider your position.

A: It's cases like these where it's important to remember the golden rules of optimization:

*

*Don't Do It

*For Experts: Don't do it yet

You should absolutely not worry about "abusing" linq unless you can indentify it explicitly as the cause of a performance problem

A: Like anything, it can be abused. As long as you stay away from obvious poor decisions such as
var v = List.Where(...);
for(int i = 0; i < v.Count(); i++)
{...}

and understand how differed execution works, then it is most likely not going to be much slower than the longhand way. According to Anders Hejlsburg (C# architect), the C# compiler is not particularly good at optimizing loops, however it is getting much better at optimizing and parallelizing expression trees. In time, it may be more effective than a loop. The List<>'s ForEach version is actually as fast as a for loop, although I can't find the link that proves that.
P.S. My personal favorite is ForEach<>'s lesser known cousin IndexedForEach (utilizing extension methods)
List.IndexedForEach( (p,i) =>
 {
     if(i != 3)
        p.DoSomething(i);
 };

A: LINQ can be like art. Keep using it to make the code beautiful.

A: I have to agree with your view - if it's more efficient to write and elegant then what's a few milliseconds.  Writing extra code gives more room for bugs to creep in and it's extra code that needs to be tested and most of all it's extra code to maintain.  Think about the guy who's going to come in behind you and maintain your code - they'll thank you for writing elegant easy to read code long before they thank you for writing code that's a few ms faster!
Beware though, this cost of a few ms could be significant when you take the bigger picture into account.  If that few milliseconds is part of a loop of thousands of repetitions, then the milliseconds add up fast.

A: You're answering your own question by talking about writing 2-3 times more code for a few ms of performance. I mean, if your problem domain requires that speedup then yes, if not probably not. However, is it really only a few ms of performance or is it > 5% or > 10%. This is a value judgement based on the individual case.

A: Where to draw the line?
Well, we already know that it is a bad idea to implement your own quicksort in linq, at least compared to just using linq's orderby.

A: I've found that using LINQ has speed up my development and made it easier to avoid stupid mistakes that loops can introduce.  I have had instances where the performance of LINQ was poor, but that was when I was using it to things like fetch data for an excel file from a tree structure that had millions of nodes.

A: While I see how there is a point of view that LINQ might make a statement harder to read, I think it is far outweighed by the fact that my methods are now strictly related to the problems that they are solving and not spending time either including lookup loops or cluttering classes with dedicated lookup functions.
It took a little while to get used to doing things with LINQ, since looping lookups, and the like, have been the main option for so long.  I look at LINQ as just being another type of syntactic sugar that can do the same task in a more elegant way.  Right now, I am still avoiding it in processing-heavy mission critical code - but that is just until the performance improves as LINQ evolves.

A: My only concern about LINQ is with its implementation of joins.
As I determined when trying to answer this question (and it's confirmed here), the code LINQ generates to perform joins is (necessarily, I guess) naive:  for each item in the list, the join performs a linear search through the joined list to find matches.
Adding a join to a LINQ query essentially turns a linear-time algorithm into a quadratic-time algorithm.  Even if you think premature optimization is the root of all evil, the jump from O(n) to O(n^2) should give you pause.  (It's O(n^3) if you join through a joined item to another collection, too.)
It's relatively easy to work around this.  For instance, this query:
var list = from pr in parentTable.AsEnumerable()
join cr in childTable.AsEnumerable() on cr.Field<int>("ParentID") equals pr.Field<int>("ID")
where pr.Field<string>("Value") == "foo"
select cr;

is analogous to how you'd join two tables in SQL Server.  But it's terribly inefficient in LINQ:  for every parent row that the where clause returns, the query scans the entire child table.  (Even if you're joining on an unindexed field, SQL Server will build a hashtable to speed up the join if it can.  That's a little outside LINQ's pay grade.)
This query, however:
string fk = "FK_ChildTable_ParentTable";
var list = from cr in childTable.AsEnumerable()
where cr.GetParentRow(fk).Field<string>("Value") == "foo"
select cr;

produces the same result, but it scans the child table once only.
If you're using LINQ to objects, the same issues apply:  if you want to join two collections of any significant size, you're probably going to need to consider implementing a more efficient method to find the joined object, e.g.:
Dictionary<Foo, Bar> map = buildMap(foos, bars);
var list = from Foo f in foos
where map[f].baz == "bat"
select f;


Q: Spring batch : Job instances run sequentially when using annotaitons I have a simple annotation configuration for a Spring batch job as follows :
@Configuration
@EnableBatchProcessing
public abstract class AbstractFileLoader<T> {

    private static final String FILE_PATTERN = "*.dat";

    @Bean
    @StepScope
    @Value("#{stepExecutionContext['fileName']}")
    public FlatFileItemReader<T> reader(String file) {
        FlatFileItemReader<T> reader = new FlatFileItemReader<T>();
        String path = file.substring(file.indexOf(":") + 1, file.length());
        FileSystemResource resource = new FileSystemResource(path);
        reader.setResource(resource);
        DefaultLineMapper<T> lineMapper = new DefaultLineMapper<T>();
        lineMapper.setFieldSetMapper(getFieldSetMapper());
        DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer(",");
        tokenizer.setNames(getColumnNames());
        lineMapper.setLineTokenizer(tokenizer);
        reader.setLineMapper(lineMapper);
        reader.setLinesToSkip(1);
        return reader;
    }

    @Bean
    public ItemProcessor<T, T> processor() {
        // TODO add transformations here
        return null;
    }

    //Exception when using JobScope for the writer
    @Bean

    public ItemWriter<T> writer() {
        ListItemWriter<T> writer = new ListItemWriter<T>();
        return writer;
    }

    @Bean
    public Job loaderJob(JobBuilderFactory jobs, Step s1,
            JobExecutionListener listener) {
        return jobs.get(getLoaderName()).incrementer(new RunIdIncrementer())
                .listener(listener).start(s1).build();
    }

    @Bean
    public Step readStep(StepBuilderFactory stepBuilderFactory,
            ItemReader<T> reader, ItemWriter<T> writer,
            ItemProcessor<T, T> processor, TaskExecutor taskExecutor,
            ResourcePatternResolver resolver) {

        final Step readerStep = stepBuilderFactory
                .get(getLoaderName() + " ReadStep:slave").<T, T> chunk(25254)
                .reader(reader).processor(processor).writer(writer)
                .taskExecutor(taskExecutor).throttleLimit(16).build();

        final Step partitionedStep = stepBuilderFactory
                .get(getLoaderName() + " ReadStep:master")
                .partitioner(readerStep)
                .partitioner(getLoaderName() + " ReadStep:slave",
                        partitioner(resolver)).taskExecutor(taskExecutor)
                .build();

        return partitionedStep;

    }

    @Bean
    public TaskExecutor taskExecutor() {
        return new SimpleAsyncTaskExecutor();
    }

    @Bean
    public Partitioner partitioner(
            ResourcePatternResolver resourcePatternResolver) {
        MultiResourcePartitioner partitioner = new MultiResourcePartitioner();
        Resource[] resources;
        try {
            resources = resourcePatternResolver.getResources("file:"
                    + getFilesPath() + FILE_PATTERN);
        } catch (IOException e) {
            throw new RuntimeException(
                    "I/O problems when resolving the input file pattern.", e);
        }
        partitioner.setResources(resources);
        return partitioner;
    }

    @Bean
    public JobExecutionListener listener(ItemWriter<T> writer) {
        /* org.springframework.batch.core.scope.StepScope scope; */
        return new JobCompletionNotificationListener<T>(writer);
    }

    public abstract FieldSetMapper<T> getFieldSetMapper();

    public abstract String getFilesPath();

    public abstract String getLoaderName();

    public abstract String[] getColumnNames();

}

When I run the same instance of the job with two different job parameters, both instances run sequentially instead of running in parallel. I have a SimpleAysncTaskExecutor bean configured which I assume should cause the jobs to be triggered asynchronously.
Do I need to add any more configuration to this class to have the job instances execute in parallel?

A: You have to configure the jobLauncher that you're using to launch jobs to use your TaskExecutor (or a separate pool). The simplest way is to override the bean:
@Bean
JobLauncher jobLauncher(JobRepository jobRepository) {
    new SimpleJobLauncher(
            taskExecutor: taskExecutor(),
            jobRepository: jobRepository)
}

Don't be confused by the warning that will be logged saying that a synchronous task executor will be used. This is due to an extra instance that is created owing to the very awkward way Spring Batch uses to configure the beans it provides in SimpleBatchConfiguration (long story short, if you want to get rid of the warning you'll need to provide a BatchConfigurer bean and specify how 4 other beans are to be created, even if you want to change just one).
Note that it being the same job is irrelevant here. The problem is that by default the job launcher will launch the job on the same thread.


Q: Connection phonegap/cordova I took this model of the Phonegap site and yet he does not issue an alert showing the connection status.
I want to open the InAppBrowser I'm creating it if it shows an internet connected via 3g or wifi and if not it will issue an alert warning the user.
Example: Example Connection Phonegap site
<!DOCTYPE html>

    <title>navigator.connection.type Example</title>

A dialog box will report the network state.


Q: Creating a writeable DocumentFile from URI I'm trying to adapt a File-based document system to something using DocumentFile in order to allow external storage read/write access on API >= 29.
I get the user to select the SD card root using Intent.ACTION_OPEN_DOCUMENT_TREE, and I get back a Uri as expected, which I can then handle using:
    getContentResolver().takePersistableUriPermission(resultData.getData(),
                            Intent.FLAG_GRANT_READ_URI_PERMISSION
                            | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

I can browse successfully through the external storage contents up to the selected root. All good.
But what I need to be able to do is write an arbitrary file in the chosen (sub)folder, and that's where I'm running into problems.
    DocumentFile file = DocumentFile.fromSingleUri(mContext, Uri.parse(toPath));
    Uri uri = file.getUri();
    FileOutputStream output = mContext.getContentResolver().openOutputStream(uri);

Except on the openOutputStream() call I get:
java.io.FileNotFoundException: Failed to open for writing: java.io.FileNotFoundException: open failed: EISDIR (Is a directory)
That's slightly confusing to me, but the "file not found" part suggests I might need to create the blank output file first, so I try that, like:
    DocumentFile file = DocumentFile.fromSingleUri(mContext, Uri.parse(toPath));
    Uri uri = file.getUri();
    if (file == null) {
        return false;
    }
    if (file.exists()) {
        file.delete();
    }
    DocumentFile.fromTreeUri(mContext, Uri.parse(getParentPath(toPath))).createFile("", uri.getLastPathSegment());
    FileOutputStream output = mContext.getContentResolver().openOutputStream(uri);

I get a java.io.IOException:
    java.lang.IllegalStateException: Failed to touch /mnt/media_rw/0B07-1910/Testing.tmp: java.io.IOException: Read-only file system
    at android.os.Parcel.createException(Parcel.java:2079)
    at android.os.Parcel.readException(Parcel.java:2039)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:188)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
    at android.content.ContentProviderProxy.call(ContentProviderNative.java:658)
    at android.content.ContentResolver.call(ContentResolver.java:2042)
    at android.provider.DocumentsContract.createDocument(DocumentsContract.java:1327)
    at androidx.documentfile.provider.TreeDocumentFile.createFile(TreeDocumentFile.java:53)
    at androidx.documentfile.provider.TreeDocumentFile.createFile(TreeDocumentFile.java:45)

Which doesn't make sense to me, since the tree should be writeable.
For what it's worth, the Uri I get back from Intent.ACTION_OPEN_DOCUMENT_TREE looks like this:
content://com.android.externalstorage.documents/tree/0B07-1910%3A
Interestingly, when I use that Uri to create a DocumentFile object to browse, using documentFile = DocumentFile.fromTreeUri(context, uri), then documentFile.getURI().toString() looks like:
content://com.android.externalstorage.documents/tree/0B07-1910%3A/document/0B07-1910%3A
i.e., it's had something appended to the end of it.
Then, I descend into what should be a writeable folder (like "Download"), and try creating a writeable file as described above. The "Download" folder gets the Uri:
content://com.android.externalstorage.documents/tree/0B07-1910%3A/document/0B07-1910%3ADownload
and the Uri I'm using for toPath, above, is then:
content://com.android.externalstorage.documents/tree/0B07-1910%3A/document/0B07-1910%3ADownload/Testing.tmp
which leads to the problems described previously trying to create it.
I haven't actually found any decent information about writing an arbitrary file under Storage Access Framework restrictions.
What am I doing wrong? Thanks. :)

A: Uri uri = uri obtained from ACTION_OPEN_DOCUMENT_TREE

String folderName = "questions.59189631";

DocumentFile documentDir = DocumentFile.fromTreeUri(context, uri);

DocumentFile folder = documentDir.createDirectory(folderName);

return folder.getUri();

Use createFile() for a writable file.


Q: Rails - show item price and add to total price I have a form where it displays the items in a select2 drop down box but I would like to also display the price for the item and enter a quantity amount that will show the total price. It would be nice to use select2 gem with these added features but I am not sure how to do this.
I have to know which item is selected and then pull the price from the table for it.
Form
<%= simple_form_for(@order) do |f| %>
  <%= f.error_notification %>
    <%= f.input :code %>

    <%= f.association :items, collection: Item.all, label_method: :name, value_method: :id, prompt: "Choose an item", input_html: { id: 'item-select2' } %>
    <%= f.submit %>
<% end %>

Talbe
  create_table "items", force: true do |t|
    t.string   "name"
    t.decimal  "price"
    t.integer  "quantity"
    t.decimal  "discount"
  end
  create_table "orders", force: true do |t|
    t.string   "code"
    t.integer  "user_id"
  end
  create_table "order_items", force: true do |t|
    t.integer  "item_id"
    t.integer  "order_id"
    t.integer  "quantity"
    t.decimal  "price"
  end

A: You have to use AJAX.
Let say we have a form (order) where we have a table (order_items). We will add rows with some goodies (items), their price and quantity. Let assume that an user already opened new order and added a new row. In the row we put the select with items, span price and text field quantity. Under table we have span total

*

*User selects item. When item is selected we call 'on_change' javascript event for this select.

*Inside that call we sent AJAX request to items controller (method 'show') with id of selected item. Inside the controller#show we find our item and return it to client as json.

*On client we have an javasript object item. Using javascript we place item.price inside span price.

*Another 'on_change' we must call for quantity text field. When quantity changed we iterate all rows, calc sum for each row and accumalate it in result vatiable. Then we put this result in total span.


Q: Expression Engine Advanced Search - Problems searching for keyword I'm having a little trouble with expression engine searching! I've got a drop down form set-up that works great, however I need to add an OPTIONAL keyword field into this search form. Any ideas how i'll do this with my current code:
Main form code:
<form method="post" action="/properties/search-results/">

Keywords:

            <input id="keywords" type="text" name="keywords"/>

Town:

            <select id="town" name="cat[]" multiple="multiple">
                <option value="" selected="selected">Any</option>
                {exp:channel:categories channel="property" style="linear" category_group="1"}
                <option value="{category_id}">{category_name}</option>
                {/exp:channel:categories}
            </select>

Property Type:

            <select id="propertyType" name="cat[]">
                <option value="" selected="selected">Any</option>
                {exp:channel:categories channel="property" style="linear" category_group="2"}
                <option value="{category_id}">{category_name}</option>
                {/exp:channel:categories}
            </select>

            <input style="margin-top:20px; width: 100px;" type="submit" name="submit" value="Search"/>
        </form>

Search-results template:
<?php
// Grab the categories selected from the $_POST
// join them with an ampersand - we are searching for AND matches
$cats = "";
foreach($_POST['cat'] as $cat){
// check we are working with a number
if(is_numeric($cat)){
$cats .= $cat."&";
}
}
// strip the last & off the category string
$cats = substr($cats,0,-1);
?>

{exp:channel:entries channel="property" dynamic="on" category="<?php echo($cats);?>" orderby="date" sort="asc"}

I need the keyword field to search in the {title} of my entries!
Thanks for any help!

A: Try this: first install the Search Fields plugin. (You need this because the native EE "search:field_name" parameter only works on custom fields, not entry titles.)
Then use this revised code:
<?php
// Grab the categories selected from the $_POST
// join them with an ampersand - we are searching for AND matches
$cats = array();
foreach($_POST['cat'] as $cat)
{
    // check we are working with a number
    if(is_numeric($cat))
    {
        $cats[] = $cat;
    }
}
$cats = implode('&', $cats);

if(!empty($_POST['keywords']))
{
    $keywords = trim($_POST['keywords']);
}
?>

<?php if($keywords) : ?>
{exp:search_fields search:title="<?php echo($keywords);?>" channel="property" parse="inward"}
<?php endif; ?>
    {exp:channel:entries channel="property" <?php if($keywords) : ?>entry_id="{search_results}"<?php endif; ?> category="<?php echo($cats);?>" orderby="date" sort="asc"}
        {!-- do stuff --}
    {/exp:channel:entries}
<?php if($keywords) : ?>
{/exp:search_fields}
<?php endif; ?>


Q: Tail recursion and call by name / value Learning Scala and functional programming in general. In the following tail-recursive factorial implementation:
def factorialTailRec(n: Int) : Int = {

    @tailrec
    def factorialRec(n: Int, f: => Int): Int = {
      if (n == 0) f else factorialRec(n - 1, n * f)
    }

    factorialRec(n, 1)
}

I wonder whether there is any benefit to having the second parameter called by value vs called by name (as I have done). In the first case, every stack frame is burdened with a product. In the second case, if my understanding is correct, the entire chain of products will be carried over to the case if ( n== 0) at the nth stack frame, so we will still have to perform the same number of multiplications. Unfortunately, this is not a product of form a^n, which can be calculated in log_2n steps through repeated squaring, but a product of terms that differ by 1 every time. So I can't see any possible way of optimizing the final product: it will still require the multiplication of O(n) terms.
Is this correct? Is call by value equivalent to call by name here, in terms of complexity?

A: Let me just expand a little bit what you've already been told in comments.
That's how by-name parameters are desugared by the compiler:
@tailrec
def factorialTailRec(n: Int, f: => Int): Int = {
  if (n == 0) {
    val fEvaluated = f
    fEvaluated
  } else {
    val fEvaluated = f // <-- here we are going deeper into stack.
    factorialTailRec(n - 1, n * fEvaluated)
  }
}

A: Through experimentation I found out that with the call by name formalism, the method becomes... non-tail recursive! I made this example code to compare factorial tail-recursively, and factorial non-tail-recursively:
 package example

 import scala.annotation.tailrec

 object Factorial extends App {

  val ITERS = 100000

  def factorialTailRec(n: Int) : Int = {
    @tailrec
    def factorialTailRec(n: Int, f: => Int): Int = {
      if (n == 0) f else factorialTailRec(n - 1, n * f)
    }
    factorialTailRec(n, 1)
  }

  for(i <-1 to ITERS) println("factorialTailRec(" + i + ") = " + factorialTailRec(i))

  def factorial(n:Int) : Int = {
    if(n == 0) 1 else n * factorial(n-1)
  }

  for(i <-1 to ITERS) println("factorial(" + i + ") = " + factorial(i))

}

Observe that the inner tailRec function calls the second argument by name.  for which the @tailRec annotation still does NOT throw a compile-time error!
I've been playing around with different values for the ITERS variable, and for a value of 100,000, I receive a... StackOverflowError!

(The result of zero is there because of overflow of Int.)
So I went ahead and changed the signature of factorialTailRec/2, to:
def factorialTailRec(n: Int, f: Int): Int

i.e call by value for the argument f. This time, the portion of main that runs factorialTailRec finishes absolutely fine, whereas, of course, factorial/1 crashes at the exact same integer.
Very, very interesting. It seems as if call by name in this situation maintains the stack frames because of the need of computation of the products themselves all the way back to the call chain.


Q: filing in TRUE/FALSE in column of dataframe A based on information in dataframe B I searched for other topics, but didn't find anything which was really matching what I was looking for.
My dataframes:
UN_match excerpt:
country country_code   emissions sector_code     year
Austria           AT  65779.1172        1.AA     2005
Austria           AT  62430.4336        1.AA     2006
Austria           AT  59108.4180        1.AA     2007
Austria           AT  58656.6719        1.AA     2008
Austria           AT  55252.9922        1.AA     2009
Austria           AT  58317.9570        1.AA     2010
Austria           AT  55898.7344        1.AA     2011
Austria           AT  53886.8242        1.AA     2012
Austria           AT  53923.7578        1.AA     2013
Austria           AT  50087.0000        1.AA     2014
Austria           AT  51978.9609        1.AA     2015
Austria           AT  52990.2305        1.AA     2016
Belgium           BE 103917.1484        1.AA     2005
Belgium           BE 102263.9297        1.AA     2006
Belgium           BE 100104.8906        1.AA     2007
Belgium           BE  99960.6328        1.AA     2008
Belgium           BE  92900.2188        1.AA     2009
Belgium           BE  96538.8047        1.AA     2010
Belgium           BE  87202.2188        1.AA     2011
Belgium           BE  86242.7656        1.AA     2012
Belgium           BE  86289.1562        1.AA     2013
Belgium           BE  80720.1406        1.AA     2014
Belgium           BE  84283.8438        1.AA     2015
Belgium           BE  84081.2031        1.AA     2016

ETS_match
     country country_code     value year smaller1.AA
     Austria           AT  16539.659 2005           0
     Austria           AT  15275.065 2006           0
     Austria           AT  14124.646 2007           0
     Austria           AT  14572.511 2008           0
     Austria           AT  12767.555 2009           0
     Austria           AT  15506.112 2010           0
     Austria           AT  15131.551 2011           0
     Austria           AT  13121.434 2012           0
     Austria           AT   8074.514 2013           0
     Austria           AT   6426.135 2014           0
     Austria           AT   7514.263 2015           0
     Austria           AT   7142.937 2016           0
     Belgium           BE  25460.856 2005           0
     Belgium           BE  24099.282 2006           0
     Belgium           BE  23706.084 2007           0
     Belgium           BE  23166.180 2008           0
     Belgium           BE  21185.552 2009           0
     Belgium           BE  22073.616 2010           0
     Belgium           BE  18950.876 2011           0
     Belgium           BE  17463.388 2012           0
     Belgium           BE  16728.267 2013           0
     Belgium           BE  15230.243 2014           0
     Belgium           BE  16053.800 2015           0
     Belgium           BE  15027.777 2016           0

I want to add TRUE or FALSE into ETS_match$smaller1.AA based on whether ETS_match$value < UN_match$emissions is true or false. This should be done for every row.
I tried some things myself with mutate and if_else, but didn't manage to finish it.
It should look like this:
     country country_code     value year smaller1.AA
     Austria           AT  16539.659 2005       TRUE
     Austria           AT  15275.065 2006       TRUE
     Austria           AT  14124.646 2007       TRUE

I know this is probably very basic, but I would be happy about any kind of help.
Best wishes,
nordsee

A: Something like this, with dplyr, maybe:
library(dplyr)
UN_match %>% left_join(ETS_match) %>%                                 # join the data
  mutate(smaller1.AA =  value < emissions, TRUE, FALSE) %>%           # add the true false
  select(country, country_code, value, year, smaller1.AA)             # only useful columns

But all your value are < of emission, so the data are rows are all TRUE in this case.
Improved removing if_else, thanks @Rui Barradas.

A: If the datasets have the same number of rows and all the rows align correctly, you can just do:
ETS_match$smaller1.AA <- ETS_match$value < UN_match$emissions

A: Because the data you're working with are from two different data frames, you have either two options:

*

*Compare them independently

*Combine the data into one data frame.

For 1, you can do something like
ETS_match$smaller1.AA <- ETS_match$value < UN_match$emissions

For 2, you could use merge to bring the data frames together.
How to join (merge) data frames (inner, outer, left, right)?


Q: TFS 2008 Merge/Resolve Conflict Tutorial I am just wondering if anyone know of a good resource/tutorial/video for explaining the "Resolve Conflict" and the "Merge Tool" in TFS 2008.
I just need to know how the comparison between files is done (I think it's comparing version number to version number), but it's not very easy to explain.
thanks!!

A: I am not sure what you mean when you say "files in drawn."  Did you mean to say "files is drawn" as in "how does TFS know how to compare files?
Resolve conflict tool is used when TFS cannot resolve the conflict on its own.
This MS Article will walk you through how to get more detailed information and explain how the tool works.
There are a few "buckets" for conflicts (see below).
As for wanting video tutorials, there are a few that simply show you how to use the tool and some cursory conflicts but there are no videos that I have found that go over each conflict case type.
Conflicts are always difficult when they can't be automatically managed.  I would consider swapping out your merge tool to a better one.
I hope that helps you.

Version Conflict
Version conflicts can occur in Team
  Foundation version control with a
  check-in, get, or merge operation. In
  each case, the evolution of an item
  along divergent paths results in a
  conflict.
  Check-in   Two users check out the latest version of a file. The

first user checks in changes; this
  creates a new version of the file.
  When the second user tries a check-in,
  there is a version conflict because
  the second user's changes were not
  made against the latest version of the
  file.
      *
  Get   Two users check out the latest version of a file. The first

user checks in changes; this creates a
  new version of the file. When the
  second user performs a get latest
  operation, there is a version conflict
  because the get latest operation is
  trying to update the checked-out file
  in the workspace.
      *
  Merge   A branched file has been modified in both branches. A user

tries to merge changes from one branch
  to the other. There is a version
  conflict because the file has been
  modified on both branches.
File Name Collision Conflict
File name collisions can occur in Team
  Foundation version control with a
  check-in, get, or merge operation. In
  all three cases, the conflict results
  when two or more items try to occupy
  the same path in the source control
  server.
  Check-in   Two users each add a file to the same application.

Coincidentally, the two users choose
  the same name for the new files. One
  user checks in his or her file. When
  the second user tries a check-in,
  there is a file name collision.
      *
  Get   Two users add files with identical names to an application. One

user checks in the file. When the
  second user tries a get latest
  operation, there is a file name
  collision. This is because the first
  user's file cannot be retrieved where
  the second user has added a file.
      *
  Merge   An application has been branched and has then been worked on

in both branches. In both branches, a
  file that has the same name has been
  added. A user tries to merge changes
  from one branch to the other. There is
  a file name collision because the file
  added to the source branch can not be
  branched where a file has already been
  added to the target branch.
Local overwrite conflict
Local overwrite conflicts only occur
  in Team Foundation version control
  during a get operation. These
  conflicts occur when a get operation
  tries to write over a writable file in
  your workspace. By default, the get
  operation will only replace files that
  are read-only. Resolving local
  overwrite conflicts involves either
  overwriting the file or checking out
  the file and merging changes.


Q: Java code Optimization and Safe class Testing{
    private static int counter;
    private static int[] intArray;
    public static ReturnClassName className(File f){
        ReturnClassName returnCN= new ReturnClassName();
        byte[] b;
        try{
            DataInputStream dataIStream= new DataInputStream(new FileInputStream(f));
            intArray= new int[dataIStream.available()];
            b= new byte[dataIStream.available()];
            dataIStream.read(b);
            intArray= b;
            // setting methods for ReturnClassName
            // counter increment
            returnCN.setNumber(someMethod(5));
            }//catch() block

    return returnCN;
}
private static int[] someMethod(int l){
    return Arrays.copyOfRange(intArray, counter, counter + l);
}

Or
class Testing{
    private static int counter;
    public static ReturnClassName className(File f){
        ReturnClassName returnCN= new ReturnClassName();
        byte[] b;
        try{
            DataInputStream dataIStream= new DataInputStream(new FileInputStream(f));
            intArray= new int[dataIStream.available()];
            b= new byte[dataIStream.available()];
            dataIStream.read(b);
            intArray= b;
            // setting methods for ReturnClassName
            // counter increment
            returnCN.setNumber(someMethod(intArray,5));

    }//catch() block

    return returnCN;
}

private static int[] someMethod(int[] iArray, int l){

    return Arrays.copyOfRange(iArray, counter, counter + l);
}

I want to know which one is more optimized and safe of the above two codes.
Also while passing the array in the 2nd code, is it passing the whole array or just the address of that array. Like both intArray and iArray are pointing to the same integer array?

A: Arrays are passed by reference so both snippets are equivalent concerning efficiency except for the fact that if you are not using intArray for some other purpose: The second version will unreference the array and make it a candidate for garbage collection.
This is, in the second case, the array will be a candidate to be collected as soon as someMethod execution returns whereas the first version will keep the array referenced until the program ends since it is static.
From your comments I understand that you will call className once per file for different files and for each file you will call 'someMethod' many times. Then I like a solution similar to the firstone at some points but different to both the first and the second one.
That solution is to have a instance of Testing for each file you load data from:

*

*Force each instance to be associated with a concrete file.

*Make methods and attributes not static. This is for each Testing element to have its own data loaded from its file.

*Change className so it will load data from its file only once.

*Make a right user of Testing and its instances.
class Testing{

    public Testing(File f)
    {
        this.f = f;
    }

    private File f;
    private int[] intArray;
    public static ReturnClassName className(){
        ReturnClassName returnCN= new ReturnClassName();
        byte[] b;
        if(intArray == null || intArray.length > 0) return //If it was called before, then we don't load the file again.
        {
            try{
                DataInputStream dataIStream= new DataInputStream(new FileInputStream(f));
                intArray= new int[dataIStream.available()];
                b = new byte[dataIStream.available()];
                dataIStream.read(b);
                intArray= b;
                // setting methods for ReturnClassName
                // counter increment
            } catch(Exception e) {
            ...
            ...
            }
        }
        returnCN.setNumber(someMethod(5));
        return returnCN;
    }

    private int[] someMethod(int l){
        return Arrays.copyOfRange(intArray, counter, counter + l);
    }
}

Example of use:
Testing forFile1 = new Testing(fileObj01);
ReturnClassName x = ReforFile1.className();
ReturnClassName y = ReforFile1.className();

Testing forFile2 = new Testing(fileObj02);
ReturnClassName z = ReforFile2.className();
ReturnClassName w = ReforFile2.className();

You could, on the other hand, implement a better solution were you have a map of integer arrays indexed by the input file (like a cache) and you keep a copy if their bytes on it. Having thus a single instance od Testing and keep File f as input parameter for 'className'.


Q: Web Development, Domain and Site Hosting I'm not 100% sure if I'm on the correct section but i'll transfer the post once I know where it's supposed to be but I am looking for advice regarding web development, setting up a domain and hosting it.
I'm currently working on a personal project using Cloud9. Which is an online development environment where I can develop and host. The reason why I chose this is because it has MySQL usability as I retrieve and store data on the project I am working on.
I am now at the point where I want to get a domain and host a site to transfer my project over to a live website with my own domain which also has SQL/MySQL usability.
Any advice or where would be the best place to look up on methods of pursuing this?
Edit: I develop using PHP and JavaScript with MySQL

A: To start I would suggest heroku, they have a free option and some nice guides, depending on which server side language you use . This way you can get used to hosting some apps and doing deployments, seeing logs etc.
The database doesn't have to be on the same hosting necessarily, you can use mongolab for example.
For domain names it's a different thing,  you will have to use the likes of godaddy

A: Why not setup your own VPS (Virtual Private Server)? There are many providers..

A: Design the software application with portability and avoid vendor lock-in to cloud services. For file transfer, there are FTP/FXP, zip/gzip, & version control standards like Git, CVS, SVN, etc. Use phpMyAdmin for the database export & import process to change web hosts. Otherwise, build a staging subdomain or a local development environment with copies of the original web app & use those to transfer files+DB to a new host for production.


Q: EPiServer XForm editor reverts default value on radio buttons on save When editing a EPiServer XForm (in the CMS) it seems that the XForm editor does not save the default settings state when working with radio buttons.
What I do:
I begin editiong a XForm and, in the XForm editor preview window, I select a radio button collection with has two options ("Private" And "Corporate") with "Private" checked as default.
I uncheck "Private" as default (since neither of the two options are to be pre-selected) and press "Save" to save the radio button collection field. The XForm preview updates correctly and shows that no radio button is checked.
But when I try to save the entire form and the XForm preview is reloaded, the radio button collection I just edited reverts back and "Private" is again pre-checked!
Any idea why this happens?
The form is implemented in a Block container and runs in a EPiServer CMS 7.5 MVC.

A: Found the anwser myself; the error occurs if the value-fields in the radion button collection is left blank.
So make sure your value-fields has some kind of value entered, when appropiate.


Q: MySQL Column 'id' in IN/ALL/ANY subquery is ambiguous I am trying to do a search functionalities that involves three tables.
Searching for users and returning wheather the user id 1 is a friend of the returned users. Also The returned users is being filtered from a third table where it checks tag of that users.
So I can say, "Return users who has tag 'Programming', 'Php'
 in userinterests table and also if the returned user is a friend of usr id 1 or not "
I am trying to use the bellow query but getting Column 'id' in IN/ALL/ANY subquery is ambiguous
If I remove the left join then it works.
SELECT n.id, n.firstName, n.lastName, t.id, t.tag, t.user_id, if(id in (
        SELECT u.id as id from friends f, users u
        WHERE CASE
        WHEN f.following_id=1
        THEN f.follower_id = u.id
        WHEN f.follower_id=1
        THEN f.following_id = u.id
        END
        AND
        f.status= 2
    ), "Yes", "No") as isFriend
FROM users n
LEFT JOIN userinterests t on  n.id = t.id

WHERE t.tag in ('Programming', 'Php')

Thank you for your time :)

A: Qualify all your column names.  You seem to know this, because all other column names are qualified.
I'm not sure if your logic is correct, but you can fix the error by qualifying the column name:
SELECT . . .
       (CASE WHEN n.id IN (SELECT u.id as id
                           FROM friends f CROSS JOIN
                                users u
                           WHERE CASE WHEN f.following_id=1
                                      THEN f.follower_id = u.id
                                      WHEN f.follower_id=1
                                      THEN f.following_id = u.id
                                 END
                           ) AND
                 f.status= 2
             THEN 'Yes' ELSE 'No'
        END) as isFriend
. . .

A: This is the way I will go for your approach:
1) I used INNER JOIN instead of LEFT JOIN for skip users that are not related to tags: Programming and Php.
2) I replaced the logic to find the set of friends related to user with id equal to 1.
SELECT
    n.id,
    n.firstName,
    n.lastName,
    t.id,
    t.tag,
    t.user_id,
    IF(
        n.id IN (SELECT follower_id FROM friends WHERE status = 2 AND following_id = 1
                 UNION
                 SELECT following_id FROM friends WHERE status = 2 AND follower_id = 1),
        "Yes",
        "No"
    ) AS isFriend
FROM
    users n
INNER JOIN
    userinterests t ON n.id = t.id AND t.tag IN ('Programming', 'Php')

Just curious, whats is the meaning of status = 2 ?


Q: Admob closes app after closing ad? I have one Activity in my app. Now I wanted to add an Admob interstitial banner. Unfortunately the old activity is also closed when the user closes the ad.
I added something like this:
 interstitial = new InterstitialAd(this, "*************");
        // Create ad request
        AdRequest adRequest = new AdRequest();
        // and begin loading your interstitial
        interstitial.loadAd(adRequest);
        interstitial.setAdListener(this);

And started the ad by using
if (interstitial.isReady()){
interstitial.show();
}

The representation of the app is working fine.
What can I do to solve the problem?

A: There is an attribute in your AndroidManifest.xml file, like android:noHistory="true" You must delete this. it solves the problem

A: Do like this
private boolean isAddShown = false;

make this isAddShown = true when the add is visible
@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    if(!isAddShown){
        super.onBackPressed();
    }else{

       isAddShown = false;
    }

}


Q: Matlab: Saving the filenames of files with specific extension, contained in a folder I'm working on a funcion to plot the data of many .csv files contained on the same folder. To do it automatically, I want to save the filenames as a string array but the only function I know I can use to get the list of files is dir and by doing
x = dir('MyFolder')

I get a struct array, not a string or char or whatever array instead. Then, I tried to save on another variable only the first column (from the 3rd row to the end) of the struct array because the filenames lay there, but I get the same struct without the first two rows.
How would you do to solve it?
Thank you in advance.

A: files = dir('*.csv') ;   % this gives all csv files present in folder
N = length(files) ;  % total number of files in the folder
for i = 1:N
    thisfile = files(i).name ;
end

In the above files is a structure, it has all the information of your csv files. You can extract name of the files using files(i).name where i = 1,2,...N.
If you want all the names of a file in a string. Use
filenames = {files.name}' ;

Above line, gives you names of all csv files in the folder into a cell array.


Q: Reshaping a Numpy Array into lexicographical list of cubes of shape (n, n, n) In order to understand what I'm trying to achieve let's imagine an ndarray a with shape (8,8,8) from which I lexicographically take blocks of shape (4,4,4). So while iterating through such blocks the indexes would look as follows:
0: a[0:4, 0:4, 0:4]
1: a[0:4, 0:4, 4:8]
2: a[0:4, 4:8, 0:4]
3: a[0:4, 4:8, 4:8]
4: a[4:8, 0:4, 0:4]
5: a[4:8, 0:4, 4:8]
6: a[4:8, 4:8, 0:4]
7: a[4:8, 4:8, 4:8]

It is these blocks of data which I'm trying to access. Obviously, this can be described by using an expression which converts the current iteration to the corresponding indexes. An example of that is given below.
a = np.ones((8,8,8))
f = 4

length = round(a.shape[0] * a.shape[1] * a.shape[2] / f**3)

x = a.shape[0] / f
y = a.shape[1] / f
z = a.shape[2] / f

for i in range(length):
    print(f"{i}: {round((int(i/(z*y))%x)*f)}:{round(f+(int(i/(z*y))%x)*f)}, {round((int(i/z)%y)*f)}:{round(f+(int(i/z)%y)*f)}, {round((i%z)*f)}:{round(f+(i%z)*f)}")

My apologies for having to do that to your eyes but it generates the following output:
0: 0:4, 0:4, 0:4
1: 0:4, 0:4, 4:8
2: 0:4, 4:8, 0:4
3: 0:4, 4:8, 4:8
4: 4:8, 0:4, 0:4
5: 4:8, 0:4, 4:8
6: 4:8, 4:8, 0:4
7: 4:8, 4:8, 4:8

So this does actually generate the right indexes, but it only allows you to access multiple blocks at once if they have the same index in the 0th and 1st axis, so no wrapping around. Ideally I would reshape this whole ndarray into an ndarray b with shape (4, 4, 32) and be ordered in such a way that b[:, :, :4] would return a[0:4, 0:4, 0:4], b[:, :, 4:12] returns an ndarray of shape (4, 4, 8) which contain a[0:4, 0:4, 4:8] and a[0:4, 4:8, 0:4] etc. I want this to be as fast as possible, so ideally, I keep the memory layout and just change the view on the array.
Lastly, if it helps to think about this conceptually, this is basically a variant of the ndarray.flatten() method but using blocks of shape (4, 4, 4) as "atomic size" if you will.
Hope this makes it clear enough!

A: It is a bit unclear what you want as output. Are you looking for this:
from skimage.util.shape import view_as_windows
b = view_as_windows(a,(f,f,f),f).reshape(-1,f,f,f).transpose(1,2,3,0).reshape(f,f,-1)

suggested by @Paul with similar result (I prefer this answer in fact):
N = 8
b = a.reshape(2,N//2,2,N//2,N).transpose(1,3,0,2,4).reshape(N//2,N//2,N*4)

output:
print(np.array_equal(b[:, :, 4:8],a[0:4, 0:4, 4:8]))
#True
print(np.array_equal(b[:, :, 8:12],a[0:4, 4:8, 0:4]))
#True
print(np.array_equal(b[:, :, 12:16],a[0:4, 4:8, 4:8]))
#True

A: def flatten_by(arr, atomic_size):
    a, b, c = arr.shape
    x, y, z = atomic_size

    r = arr.reshape([a//x, x, b//y, y, c//z, z])
    r = r.transpose([0, 2, 4, 1, 3, 5])
    r = r.reshape([-1, x, y, z])

    return r

flatten_by(arr, [4,4,4]).shape

>>>   (8, 4, 4, 4)

EDIT:

the function applies C-style flattening to the array, as shown below

NOTE:
this method and @Ehsan's method both produce "copies" NOT "views", im looking into it and would update the answer when if i find a solution

flattened = flatten_by(arr, [4,4,4])

required = np.array([
    arr[0:4, 0:4, 0:4],
    arr[0:4, 0:4, 4:8],
    arr[0:4, 4:8, 0:4],
    arr[0:4, 4:8, 4:8],
    arr[4:8, 0:4, 0:4],
    arr[4:8, 0:4, 4:8],
    arr[4:8, 4:8, 0:4],
    arr[4:8, 4:8, 4:8],
])

np.array_equal(required, flattened)

>>> True


Q: CSS fixed nav on top, when opened covers full screen, can't scroll I have a mobile navigation bar set to fixed using CSS at the top of my page so it sticks upon scrolling. When clicked, the full navigation menu is revealed and on many devices it covers the entire screen (which is what I want), but the last 25% or so of it is below the fold. Since the menu is set to fixed, it will not allow scrolling down (when open) to view the hidden content below the fold.
Basically, how do I make a fixed element scrollable once it is open and part of it is being hidden below the fold? I know the problem is that the top of the element is still attached to the top of the screen, just not how to make it temporarily "switch" over to an absolute position (which will allow the scrolling down) when the full menu opens.
Is this possible? Any help would be greatly appreciated! This is driving me nuts...


Q: Cypress test runs before Vercel deployment in Github Actions I have successfully created a script to test Vercel test deployments using Cypress integrated with Github Actions. Although the test works and the desired result is achieved, there is a slightly annoying issue-- the Cypress test runs (and is skipped) before the Vercel deployment attempt. I am employing a conditional in the GA workflow yml so that the Cypress tests run after successful test deployment, so it ends up running after the deployment. I would like to be able to omit the first skipped attempt at the Cypress test. I have tried incorporating other Github Actions to fix this, but they block the test from being run at all if the deployment is not finished. I have also tried playing with the settings of the repo, to no avail. Below is my GA yml:
name: Cypress Testing
on: [deployment_status]
jobs:
  e2e:
    if: github.event.deployment_status.state == 'success'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Setup npmrc
        run: echo "//registry.npmjs.org/:_authToken=${{secrets.NPM_AUTH_TOKEN}}" > .npmrc
      - name: Setup npm package
        run: npm init -y && npm install
      - name: Setup node 12
        uses: actions/setup-node@v1
        with:
          node-version: 12.x
      - name: Run Cypress
        uses: cypress-io/github-action@v2
        env:
          CYPRESS_BASE_URL: ${{ github.event.deployment_status.target_url }}

Our Vercel project is integrated with Git, so it deploys automatically with every push. Has anyone ever had this issue, where Cypress tries to run first before Vercel deployment?

A: What happens in your deploy:

*

*you push changes to your repo

*vercel watches your repo, see there is a new commit

*it sends the status pending to your repo and builds the stuff on vercel servers. So now your repo knows vercel is doing something and you can see that e.g. in your PR in the ckecks. => with this "Status update" your workflow is automaticaly triggered (but skipped because the condition is false), because the status changed from nothing to "pending"

*vercel has finished the deployment and sends another status update to GitHub. => your workflow is triggered again. But this time the if condition in the workflow is true and the rest will be executed.

So it is not possible to avoid the skipped workflow with the deployment_status as a trigger for the workflow.


Q: Passing arguments - interface implementation vs object of concrete class which implements the interface Considering OnCompleteListener is an interface and OnCompleteListenerImpl is a concrete class as follows
### OnCompleteListener  interface ####
public interface OnCompleteListener {

    public void onComplete();
}

#### OnCompleteListenerImpl  ####
class OnCompleteListenerImpl  implements OnCompleteListener {
    public void onComplete() {
        System.out.println("Yeah, the long running task has been completed!");
    }
}

How is this
#### Snippet A #####
longRunningTask.setOnCompleteListener(new OnCompleteListener() {

        @Override
        public void onComplete() {
            System.out.println("Yeah, the long running task has been completed!");
        }
    }
);

different from
#### Snippet B ######
OnCompleteListenerImpl obj = new OnCompleteListenerImpl();

longRunningTask.setOnCompleteListener(obj);

A: Both are approaches are similar but there is a big difference between
Usability

*

*Class OnCompleteListenerImpl is reuseable, you need not override the same object multiple places

*You need to be careful while initializing OnCompleteListenerImpl object, as you can initialize in multiple ways, you have to be careful with its scope.

*You need not create a new implementation class if you are going with an anonymous approach.

In summary, there are different use cases for both the approaches it is up to you which one you want to follow. For clean code and reusability, we go with implementation class but in case of secure and single instance use, you can go with anonymous class approach.


Q: How to fix this Loss error in pytorch multi_class model I am training a multi_class model for sentiment analyses and when I start the training I get an error msg regarding the Loss function.  I am really lost here.
from sklearn.metrics import f1_score, roc_auc_score, accuracy_score
from transformers import EvalPrediction
import torch

def multi_class_metrics(predictions, labels, threshold=0.5):
    softmax = torch.nn.Softmax()
    probs = softmax(torch.Tensor(predictions))

    y_pred = np.zeros(probs.shape)
    y_pred[np.where(probs >= threshold)] = 1

    y_true = labels
    f1_micro_average = f1_score(y_true=y_true, y_pred=y_pred, average='micro')
    roc_auc = roc_auc_score(y_true, y_pred, average = 'micro')
    accuracy = accuracy_score(y_true, y_pred)

    metrics = {'f1': f1_micro_average,
               'roc_auc': roc_auc,
               'accuracy': accuracy}
    return metrics

def compute_metrics(p: EvalPrediction):
    preds = p.predictions[0] if isinstance(p.predictions,
            tuple) else p.predictions
    result = multi_class_metrics(
        predictions=preds,
        labels=p.label_ids)
    return result

And When I start the trainning I get this error:  Loss
trainer = Trainer(
    model,
    args,
    train_dataset=encoded_dataset["train"],
    eval_dataset=encoded_dataset["validation"],
    tokenizer=tokenizer,
    compute_metrics=compute_metrics
)

trainer.train()
   218         if isinstance(k, str):
    219             inner_dict = {k: v for (k, v) in self.items()}
--> 220             return inner_dict[k]
    221         else:
    222             return self.to_tuple()[k]