texts:
Is this a good approach or I've just found a nasty workaround?

I'm using MediatorLiveData class because seems useful to update the source of a LiveData object.

I mean, the majority of tutorials that I've found on internet just use Livedata or MutableLivedata without a dynamic source, in example:

fun search(/*no input params*/): Call<List<Person>>

But in my case, I have the following web service that performs a search by name:

interface APIServidor {
    @GET("search")
    fun search(@Query("name") name: String): Call<List<Person>>

}

public class PeopleRepository {

    public LiveData<List<Person>> search(String name){

        final MutableLiveData<List<Person>> apiResponse = new MutableLiveData<>();
        Call<List<Person>> call = RetrofitService.Companion.getInstance().getApiServer().search(name);
        call.enqueue(new Callback<List<Person>>() {
            @Override
            public void onResponse(@NonNull Call<List<Person>> call, @NonNull Response<List<Person>> response) {
                if (response.isSuccessful()) {
                    apiResponse.postValue(response.body());
                }
            }

            @Override
            public void onFailure(@NonNull Call<List<Person>> call, @NonNull Throwable t) {
                apiResponse.postValue(null);
            }
        });

        return apiResponse;
    }
}

Then in the viewmodel class I'm adding source per new request.

public class SearchViewModel extends ViewModel {

    private MediatorLiveData<List<Person>> mApiResponse;
    private PeopleRepository mApiRepo;

    public SearchViewModel() {
        mApiResponse = new MediatorLiveData<>();
        mApiRepo = new PeopleRepository();
    }

    public LiveData<List<Person>> getPlayers() {
        return mApiResponse;
    }

    public void performSearch(String name){
        mApiResponse.addSource(mApiRepo.search(name), new Observer<List<Person>>() {
            @Override
            public void onChanged(List<Person> apiResponse)            {
                mApiResponse.setValue(apiResponse);
            }
        });
    }
}

Activity

bt_search.setOnClickListener {
    val player_name = et_player.text.toString()
    viewModel.performSearch(player_name)
}

Project scope

I'm in a personal project

Goals

Use MVVM + Live data + Repository pattern

Problem

I've only found tutorials with a simple approach: observe a LiveData object that access to a repository object and fetch data only once.

In example: Fetch all people (select * from people) from web service.

My case: Fetch people that mach a name (select * from people where name=?) from web service.

https://medium.com/@elye.project/kotlin-and-retrofit-2-tutorial-with-working-codes-333a4422a890
https://medium.com/@sriramr083/error-handling-in-retrofit2-in-mvvm-repository-pattern-a9c13c8f3995

Doubts

Is a good idea use MediatorLiveData class to merge all requests took from user input?

Should I use MutableLiveData and change the repository class and use a custom clousure?

Is there a better approach?


texts: Flask web application, with handling for user login status

texts:
I am trying to change some variable in base html-template which other html pages inherite from after entering user in personal account.

The idea is as follows:

there is base html template and there depends on user status (logged in or logged out) are buttons Registration and Enter/Exit.
if user is logged in then I want Enter button and Registration button to be dissappeared and instead of Enter button I want Exit button to be there
in case if user is not logged in then two buttons should be: Enter and Registration

I managed to accomplish this task using flask, jinja2 and python but my backend code looks unbearably ugly because there is too much of duplication

Here is my python code:

this function executes before user gets to any html page and determines its status

from flask import g
from flask import render_template
@app.url_value_preprocessor
def send_user_login_status(endpoint, values):
    if current_user.is_authenticated: # if logged in
        g.login_button_class = u'logout'
        g.login_button = u'Exit'

        g.signin_button = u''
        g.signin_button_class = u'signout'
    else: # if not logged in
        g.login_button_class = u'login'
        g.login_button = u'Enter'

        g.signin_button = u'Registration'
        g.signin_button_class = u'signin'

    return render_template("base.html",
                           login_button=g.login_button,
                           login_button_class=g.login_button_class,
                           signin_button=g.signin_button,
                           signin_button_class=g.signin_button_class,
                           )

And now I must every time pass variables to html page in order this page to be with needed buttons.

For example home page:

@app.route('/')
def home():
    return render_template("index.html",
                           login_button=g.login_button,
                           login_button_class=g.login_button_class,
                           signin_button=g.signin_button,
                           signin_button_class=g.signin_button_class,
                           )

or about us page:

@app.route('/about')
def about():
    # send_user_login_status()
    return render_template("about.html",
                           login_button=g.login_button,
                           login_button_class=g.login_button_class,
                           signin_button=g.signin_button,
                           signin_button_class=g.signin_button_class,
                         )

This is my python logout function:

@app.route('/logout', methods=['POST'])
def logout():
    login_status = logout_user()
    if login_status:
        g.login_button_class = u'login'
        g.login_button = u'Enter'

        g.signin_button = u'Registration'
        g.signin_button_class = u'signin'

        jsonify({'result': True})
        return redirect('/')

And as you can see there is too much of duplication because I need to pass this depends-on-login-status variables to each html page.

Whereas html code looks nice:

base.html

<!DOCTYPE html>

 <meta charset="UTF-8"/>

 {{ login_button }}

{{ signin_button }}

                {% block content %}
                {% endblock %}

index.html

{% extends "base.html" %}
{% block content %}

Home

{% endblock %}

about.html

{% extends "base.html" %}
{% block content %}

About

{% endblock %}


texts: Golang Error handling by a generic interface

texts:
I was wondering why we people don't use a generic interface instead of error type. You can see this pattern a lot in the Golang community:
func Division(a int, b int) (int, error) {
    if b == 0 {
        return 0, errors.New("divided by zero error")
    }
    return a / b, nil
}

error is not like exception (in Java), so we usually have a chain of conditional statements to check the error and stuff. In this sample I just used a generic interface for both the result and the error itself (I don't need the error type here! just nil is enough)
func Division1(a int, b int) interface{} {
    if b == 0 {
        return nil
    }
    return a / b
}

Or we can have a struct for the return type, this is more clean than to have many return types.
type Output struct {
    Result interface{}
    Error  error
}

func Division(a int, b int) Output {
    if b == 0 {
        return Output{
            Error: errors.New("divided by zero error"),
        }
    }
    return Output{
            Result: a / b,
    }
}


texts: Drawing thousands of lines in WebGL or Canvas

texts:
I am trying to draw thousands of lines between nodes, and the line positions are constantly changing (i.e. consider a node being dragged while connected to thousands of children by these lines).

Below is a simplified example of an issue I have. When drawing several thousands of lines in WebGL or Canvas, the rendering takes a long time (up to a second or more). For each line that needs to be updated, I clear the PIXI.Graphics object and then redraw it from scratch.

I was wondering if there is a way to improve this by batching the draw to WebGL somehow. Ideally the render time could be reduced significantly, so I could draw up to 100,000 lines in less than a second.

Warning: It may crash your browser tab - lower the number of objects to below 20,000 if this happens.

var container = new PIXI.Container(),
    i,
    renderer = PIXI.autoDetectRenderer(1000, 1000);
renderer.backgroundColor = 0xFFFFFF;
document.body.appendChild(renderer.view);
for (i = 0; i < 20000; i++) {
    line = new PIXI.Graphics();
    line.lineStyle(1, 0x888888, 1);
    line.moveTo(0,0);
    line.lineTo(10*i,1000);
    container.addChild(line);
}
renderer.render(container);
<script src="https://cdnjs.cloudflare.com/ajax/libs/pixi.js/3.0.8/pixi.js"></script>

Pure JavaScript/WebGL/Canvas improvements are useful as well (Pixi is not required).


texts: Ordinary Data Processing Task in Haskell: Vague Misgivings

texts:
This is an ordinary data processing task: read a list of dates and amounts (for example, deposits and withdrawals from a bank account) and report the date on which the lowest balance was recorded and what that balance was. While I do get the correct answer--always a virtue--with the code below, I can't help thinking that it could be done better (e.g. more concisely). I've included the preliminary input parsing code so that the whole thing can be compiled and run as is.

module Main where

import Text.ParserCombinators.Parsec
import Data.Time
import Numeric
import Control.Applicative ((<$>), (<*>), (*>))
import Control.Monad.State as CM

--The purpose is to sum up a list of dated values (example: transactions in a bank
--account) and produce the date and amount of the lowest recorded balance.
--For the three lines below, the output would be:
--     DtdBal {runningBal = 4859.96, lowDt = 2013-01-23, lowBal = 4859.96}

--12/1/12,  6844.79
--1/1/13,   992.41
--1/23/13,  -2977.24

data DtdVal = DtdVal { dt :: Day
                     , amt :: Double
                     } deriving Show

data DtdBal = DtdBal { runningBal :: Double
                     , lowDt :: Day
                     , lowBal :: Double
                     } deriving Show

main = do res <- parseFromFile sched "escrow.txt"
          case res of
            Right l -> putStrLn . show $ process l
            Left _ -> error "Oops"
    where process (x:xs) = execState (minBal xs) (initState x)
          initState x = DtdBal { runningBal = amt x, lowDt = dt x, lowBal = amt x }

minBal :: [DtdVal] -> CM.State DtdBal ()
minBal [] = return ()
minBal (x:xs) = do
  rBal <- runningBal <$> get
  lBal <- lowBal <$> get
  lDt  <- lowDt <$> get
  if amt x + rBal < lBal
     then put $ DtdBal { runningBal = rBal + amt x, lowDt = dt x, lowBal = amt x + rBal}
     else put $ DtdBal { runningBal = rBal + amt x, lowDt = lDt, lowBal = lBal }
  minBal xs

sched :: CharParser () [DtdVal]
sched = line `endBy` newline

line :: CharParser () DtdVal
line = DtdVal <$> dts <*> (char ',' >> spaces *> readSgnd)

dts :: CharParser () Day
dts = toDay <$> many digit `sepBy` char '/'
    where toDay [x, y, z] = fromGregorian (2000 + read z) (read x) (read y)

readSgnd :: CharParser () Double
readSgnd = do
  optional (char '+')
  inp <- getInput
  let [(num, inp')] = readSigned readFloat inp
  setInput inp'
  return num


texts: Checking if a column belongs to a provided list

texts:
I've the following code, which checks if one column belongs to the list I provide.  After that, it takes the value contained in it.

I've tried and it's 9 times faster than raising and handling an exception. But I don't find this code very readable.

For example, I don't need to define a variable drsIDAgentClient. I've thought of using an extension method over reader and provide a ColumnExist(string columnName) method.

Can you see a better solution?

DataRow[] drsIDAgentClient = reader.GetSchemaTable().Select("ColumnName = 'IDAgentClient'");
if (drsIDAgentClient.Length > 0)
    coupon.IdAgentClient = reader["IDAgentClient"] == DBNull.Value ? null : (int?)reader["IDAgentClient"];


texts: Getting location using LiveData and FusedLocationProviderClient

texts:
My goal is to get the location when the app starts, and if the user navigates away and comes back, to receive one location update and that's it. I am worried about removing the observer in the getLocationUpdates method.

BoundLocationManager.class

public class BoundLocationManager extends LiveData<Location> {

    private static BoundLocationManager instance;
    private FusedLocationProviderClient mFusedLocationClient;
    private LocationRequest mLocationRequest;

    public static BoundLocationManager getInstance(Context appContext) {
        if (instance == null) {
            instance = new BoundLocationManager(appContext);
        }
        return instance;
    }

    private BoundLocationManager(final Context appContext) {
        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(appContext);
        createLocationRequest();
        mFusedLocationClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
            @Override
            public void onComplete(@NonNull Task<Location> task) {
                if(task.isSuccessful() && task.getResult() != null) {
                    setValue(task.getResult());
                } else {
                    mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null);
                }
            }
        });

    }

    private void createLocationRequest() {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(10000);
        mLocationRequest.setFastestInterval(5000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    }

    LocationCallback mLocationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            for (Location location : locationResult.getLocations()) {
                if (location != null)
                    setValue(location);
            }
        }
    };

    @Override
    protected void onInactive() {
        super.onInactive();
        if (mLocationCallback != null)
            mFusedLocationClient.removeLocationUpdates(mLocationCallback);
    }

}

WeatherFragment.class

private void getLocationUpdates() {
        BoundLocationManager.getInstance(getActivity().getApplicationContext()).observe(this, new Observer<Location>() {
            @Override
            public void onChanged(@Nullable Location location) {
                if (location != null) {
                    getWeather(location.getLatitude(), location.getLongitude());
                    getAddress(location.getLatitude(), location.getLongitude());
                    BoundLocationManager.getInstance(getActivity().getApplicationContext()).removeObserver(this);
                } else
                    Toast.makeText(getActivity(), "Location is null", Toast.LENGTH_SHORT).show();
            }
        });
    }

And finally I use it:

@Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSIONS_REQUEST_LOCATION);
        else {
            getLocationUpdates();
        }
    }


texts: Randomly generate spelling mistake in string

texts:
My code will generate a spelling mistake inside of a string 50% of the time.

It will retrieve a letter from a random index in the string like, "t" and then duplicate that letter like, "tt" and store it in the spelling mistake variable.

It will then replace, "t" with "tt" in the string to replicate a spelling mistake. (50% of the time.)

How can I improve my code to make it complete the same task, but with less lines of code and using the least amount of resources possible?

(function() {

    function replaceStr(str, pos, value) {
        var arr = str.split('');
        arr[pos] = value;
        return arr.join('');
    }

    var myString = "Stack Overflow";
    var letterIndex = Math.floor(Math.random() * myString.length); // Example: 1
    var letter = myString.charAt(letterIndex); // Example: "t"
    var mistake = letter + letter; // Example: "tt"

    // 0 -> 9 (coin toss)
    if (Math.floor(Math.random() * 10) >= 5) {
        return replaceStr(myString, letterIndex, mistake);
    } else {
        return myString;
    }

})();

Result from 10 runs:

Stack Overflow
Stackk Overflow
Stack Overflow
Sttack Overflow
Stack Overflow
Stack Oveerflow
Stack Overrflow
Stack Overfllow
Stack Overflow
Stack Oveerflow


texts:
I currently use this code to extract the required GPU data. Can you review this method for efficiency or best coding practices?

Code

import wmi
c = wmi.WMI()
def gpu_check():
    gpu_ouy =["fu","7"]
    gpu_a =""
    gpu_b =""
    ass   = str(c.Win32_VideoController()[0])
    asss  = str(c.Win32_VideoController()[1])
    aq = ass.split()
    for x in range(len(c.Win32_VideoController())):
        ass   = str(c.Win32_VideoController()[x])
        saa = ass.split()
        saa.remove("instance")
        saa.remove("of")
        saa.remove("Win32_VideoController")
        saa.remove("{")
        saa.remove("AdapterCompatibility")
        saa.remove("AdapterDACType")
        while "=" in saa:
            saa.remove("=")
        saa.remove("};")
        saa.remove("SystemName")
        saa.remove("Description")
        saa.remove("InstalledDisplayDrivers")
        saa.remove("DeviceID")
        saa.remove("DriverDate")
        saa.remove("Name")
        saa.remove("Status")
        saa.remove("Caption")
        saa.remove("InfFilename")
        saa.remove("ConfigManagerUserConfig")
        saa.remove("ConfigManagerErrorCode")
        saa.remove("InfSection")
        saa.remove("DriverVersion")
        if "CurrentScanMode" in saa:
            saa.remove("CurrentScanMode")
        saa.remove("SystemCreationClassName")
        saa.remove("CreationClassName")
        if "CurrentRefreshRate" in saa:
            saa.remove("CurrentRefreshRate")
        if "CurrentHorizontalResolution" in saa:
            saa.remove("CurrentHorizontalResolution")
        if "CurrentVerticalResolution" in saa:
            saa.remove("CurrentVerticalResolution")
        if "CurrentNumberOfColors" in saa:
            saa.remove("CurrentNumberOfColors")
        if "MinRefreshRate" in saa:
            saa.remove("MinRefreshRate")
        if 'colors";' in saa:
            saa.remove('colors";')
        if "CurrentBitsPerPixel" in saa:
            saa.remove("CurrentBitsPerPixel")
        if "MaxRefreshRate" in saa:
            saa.remove("MaxRefreshRate")
        saa.remove("VideoProcessor")
        saa.remove("PNPDeviceID")
        if "CurrentNumberOfColumns" in saa:
            saa.remove("CurrentNumberOfColumns")
        if "CurrentNumberOfRows" in saa:
            saa.remove("CurrentNumberOfRows")
        saa.remove("Monochrome")
        saa.remove("Availability")
        del saa[5]
        if '"Integrated' in saa:
            saa.remove('"Integrated')
        if '"Internal";' in saa:
            saa.remove('"Internal";')
        if 'VideoModeDescription' in saa:
            saa.remove('VideoModeDescription')
        saa.remove('AdapterRAM')
        if 'RAMDAC";' in saa:
            saa.remove('RAMDAC";')
            sqa =int(((int(str(saa[1]).replace(";",""))/1024)/1024)/1000)
            saa[1] = sqa
        else:
            sqa =int(((int(str(saa[2]).replace(";",""))/1024)/1024)/1000)
            saa[2] = sqa
        if 'Corporation";' in saa:
            saa.remove('Corporation";')
        while "FALSE;" in saa:
            saa.remove("FALSE;")
        gpu_a =""
        gpu_b =""
        if saa[6] =="0;":
            gpu_a = str(saa[2]).replace('"',"")+" "+saa[3]+" "+saa[4]+" "+str(saa[5]).replace('";',"")+" "+str(saa[1])+" GB"
            gpu_ouy[0] = str(gpu_a)
        else:
            gpu_b = str(saa[2]).replace('"',"")+" "+saa[3]+" "+saa[4]+" "+str(saa[5]).replace('";',"")+" "+str(saa[6]).replace('";',"")+" "+str(saa[1])+" GB"
            gpu_ouy[1] = gpu_b
    return gpu_ouy


texts: Does refactoring a while loop increase CPU usage?

texts:
While upgrading our code base to take advantage of new features in .Net 4.5, I'm trying to refactor our take of the classic Producer/Consumer algorithm, but I'm concerned my refactoring is going to increase the CPU usage.

Each instance of my WorkItemConsumer class is basically a System.Threading.Thread that is able to process at most MaxConcurrentHandlers items, each of which is handled by a TPL System.Threading.Task.

In my previous implementation, I made use of explicit locking to read from the Queue<T>. Also, the class wrapping the queue set an AutoResetEvent to signal when a new item has been produced. Here was the code:

protected override void Run(string[] args)
{
    while (true)
    {
        var index = WaitHandle.WaitAny(new[] {StopRequested, queue_.ItemsAvailable});
        if (index == 0)
            break;

        if (tasks_.Count == MaxConcurrentHandlers)
            WaitAndDiscardAnyCompletedTask();

        var item = queue_.PopItem();

        // "null" denotes a signal to stop processing subsequent work items

        if (item == null)
            break;

        tasks_.Add(CreateWorkItemHandler(item));
    }

    while(tasks_.Count > 0)
        WaitAndDiscardAnyCompletedTask();
}

I would like to take advantage of the BlockingCollection<T> class, which gives me most of the features I want. It allows a consumer to know when no more items will ever be published and it also allows my to remove any explicit locking. However, I don't have a way to being signaled when new work items are available.

Here is the new code:

    protected override void Run(string[] args)
    {
        while (!queue_.IsCompleted)
        {
            if (StopRequested.WaitOne(0))
                break;

            if (tasks_.Count == MaxConcurrentHandlers)
                WaitAndDiscardAnyCompletedTask();

            foreach (var item in queue_.GetConsumingEnumerable().Take(MaxConcurrentHandlers - tasks_.Count))
                tasks_.Add(CreateWorkItemHandler(item));
        }

        while(tasks_.Count > 0)
            WaitAndDiscardAnyCompletedTask();
    }

How would you refactor this code?


texts: Pick a random file from a directory tree

texts:
I'm working on a python module to randomly choose a wallpaper for my desktop background among thousands of pictures in my photo library.

I though I'd make a general function:

get_random_file(ext, top)

  Return the name of a random file in a directory tree with top as root.

Since I'm dealing with a very large directory tree, listing all possible files would be very time consuming (edit: see comments on C. Harley answer). So I went for heuristic method, making sure all files are covered and adding an upper bound in case there are no files with given extension. There are occasionally false negatives, which is not a too big deal, but most importantly, it runs nearly instantly.

Algorithm is simple:

Get a list of directories, including .
Pick a random directory from that list.
If the picked directory is . (either randomly, or only available option), then retrieve a list of files in that directory.
If the list of files is empty, go back to 1.
Return a random file from that list.

Note that this algorithm can go many levels deep into sub-directories.

So far I'm happy with performance and returned values. It is able to quickly pick up a file in a very large tree.

However, I'm after general comments in terms of performance and also code style.

What I'm not particularly proud of in my code is the use of next(os.walk(top))[1] to get the list of sub-directories in the current directory.

There are also false negatives, when asking for a file extension with not many occurrences in a tree with many directories. However, I don't want to increase the limit too much to avoid waiting when really there are not such files in the tree.

The code:

# -*- coding: utf-8 -*-
"""
Created on Wed Aug 29 14:05:27 2018
@author: raf
"""
import os
import sys
import glob
import random

def get_random_files(ext, top):
    '''Return the name of a random file within a top path.

    Works recursivelly in subdirectories.

    Note that a full list of files could be got with
        glob(top + '/**/*.' + ext, recursive=True)

    However that would be extremely slow for large directories.
    '''
    _top = top
    ct, limit = 0, 50000
    while True:

        if ct > limit:
            return 'No file found after %d iterations.' % limit
        ct += 1

        try:
            dirs = next(os.walk(top))[1]
        except StopIteration:  # access denied and other exceptions
            top = _top
            continue

        i = random.randint(0, len(dirs))
        if i == 0:  # use .
            files = glob.glob(top + '/*.' + ext)
            if not files:
                top = _top
                continue
            i = random.randint(0, len(files)-1)
            return files[i]

        top += '/' + dirs[i-1]

if __name__ == '__main__':
    if len(sys.argv) > 1:
        print(get_random_files(sys.argv[1], os.getcwd()))
    else:
        print(get_random_files('*', os.getcwd()))


texts: PHP, Is creating a class for templates to much?

texts:
I am new to using classes heavily, and was wondering if going so far as to create a class specifically for templates was going to far. Here is a simplified version of my class template. Yes, I know I the use of unset is not necessary, but I like it there so don't give me any grief for it.

class template{
    private $templateString = "";
    private $templatePlaceholderStrings = array();
    private $templatePlaceholderValueStrings = array();

    function fillTemplateString($string){
        $this->templateString = $string;
        unset($string);
    }

    function fillTemplatePlaceholderStrings($array){
        $this->templatePlaceholderStrings = $array;
        unset($array);
    }

    function filltemplatePlaceholderValueStrings($array){
        $this->templatePlaceholderValueStrings = $arrray;
        unset($array);
    }

    function buildTemplate(){
        return preg_replace($this->templatePlaceholderStrings,
                    $this->templatePlaceholderValueStrings,
                    $this->templateString);
    }
}


texts: Creating and referencing markup....cloning, etc. efficient system for this plugin

texts:
I am pulling multiple feeds from youtube that have a callback function which makes markup from each feed. They add that markup to a documentFragment stored in var data. When all the feeds have contributed what they should, a final callback runs. Each feed can deal with its own thumbnails and page menu, so it makes sense to get that started without waiting for all of the feeds to be done. Some markup requires all of them to be done, so I do that in the final callback. To complicate matters, the markup needs to be appended in an order dictated by the user and some sets of markup may need to be cloned if requested. This allows the user to easily build the plugin as they see fit. Here is the option for that.

build:['listsMenu', 'listTitle', 'listDesc', 'player', 'pageMenu', 'thumbnails']

or another possibility...something like this:

build:['player', 'listsMenu', 'pageMenu', 'thumbnails', 'pageMenu']

This is the best solution I have been able to come up with so far. Any suggestions are appreciated!

//in the function that makes the ajax calls -
    data = {};

namespace.thumbs.prototype.feedCallback = function(context, options, data, optList, listNum, feed) {
    if (options.build.indexOf('pageMenu') !== -1 || options.build.indexOf('thumbnails') !== -1) {
        var feedVideosInPages = this.return_feedVideosInPages(options.thumbCount, options.thumbsPerPage, feed.videos);
        var pagesMenu = this.return_PagesMenu(options, listNum, feedVideosInPages);
        var thumbnails = this.return_thumbsMarkup(options, optList, listNum, feedVideosInPages);
        data.pagesMenus = data.pagesMenus || document.createDocumentFragment();
        data.thumbnails = data.thumbnails || document.createDocumentFragment();
        if (pagesMenu) { data.pagesMenus.appendChild(pagesMenu); }
        data.thumbnails.appendChild(thumbnails);
    }

    data.each = data.each || [];
    data.each.push({'feed':feed, 'optList':optList, 'listNum':listNum});

    if (data.each.length === options.lists.length) { this.allFeedsReady(context, options, data); }
};
namespace.thumbs.prototype.allFeedsReady = function(context, options, data) {
    this.sort_dataEach(data);

    elems = document.createDocumentFragment();
    addedElemNames = [];
    clonedElems = {};
    toAdd = options.build.slice();
    var buildLength = options.build.length;
    for (var i=0; i<buildLength; ++i) {
        var name = options.build[i];
        if (addedElemNames.indexOf(name) === -1) { //this is the first time the element has been added.
            switch (name) {
                case 'pageMenu': elem = data.pagesMenus; break;
                case 'thumbnails': elem = data.thumbnails; break;
                case 'player': elem = this.return_playerDiv(context); break;
                case 'listMenu': elem = this.return_listsMenuMarkup(data.each); break;
                //case 'listTitle': elem = allData.markup.listsTitles[0]; break;
                case 'listDesc': elem = this.return_listsDescsMarkup(data.each); break;
                case 'videoTitle': elem = this.return_videoTitleMarkup(data.each); break;
                    default: elem = false;
            }
            addedElemNames.push(name);
        }
        else {
            elem = clonedElems[name];
        }
        toAdd.splice(toAdd.indexOf(name), 1); //remove this item from array of things to be added
        if (elem) {
            if (toAdd.indexOf(name) !== -1) { clonedElems[name] = elem.cloneNode(true); }//the build will contain another copy, so clone it
            elems.appendChild(elem);
        }
    }
    context.appendChild(elems);
}


texts: Iterate JSON GIF collection array to HTML file

texts:
I've written a piece of code which utilizes a for loop to iterate this array in JavaScript. This is my first attempt and I wonder if there is an optimal way to do this.

Fiddle

JavaScript

var gifCollection = {
"gifs": [
  {
  "gifName" : "gif-One",
  "gifLink" : "http://buffalogrove.sat.iit.edu/thumb/turkey_karadeniz_region-t2.jpg"
 },
  {
  "gifName" : "gif-Two",
  "gifLink" : "http://buffalogrove.sat.iit.edu/thumb/turkey_karadeniz_region-t2.jpg"
 },
  {
  "gifName" : "gif-Three",
  "gifLink" : "http://buffalogrove.sat.iit.edu/thumb/turkey_karadeniz_region-t2.jpg"
 },
  {
  "gifName" : "gif-Four",
  "gifLink" : "http://buffalogrove.sat.iit.edu/thumb/turkey_karadeniz_region-t2.jpg"
 },
 {
  "gifName" : "gif-Five",
  "gifLink" : "http://buffalogrove.sat.iit.edu/thumb/turkey_karadeniz_region-t2.jpg"
 },
 {
  "gifName" : "gif-Six",
  "gifLink" : "http://buffalogrove.sat.iit.edu/thumb/turkey_karadeniz_region-t2.jpg"
 },
 {
  "gifName" : "gif-Seven",
  "gifLink" : "http://buffalogrove.sat.iit.edu/thumb/turkey_karadeniz_region-t2.jpg"
 },
 {
  "gifName" : "gif-Eight",
  "gifLink" : "http://buffalogrove.sat.iit.edu/thumb/turkey_karadeniz_region-t2.jpg"
 },
 {
  "gifName" : "gif-Nine",
  "gifLink" : "http://buffalogrove.sat.iit.edu/thumb/turkey_karadeniz_region-t2.jpg"
 },
 {
  "gifName" : "gif-Ten",
  "gifLink" : "http://buffalogrove.sat.iit.edu/thumb/turkey_karadeniz_region-t2.jpg"
  }
 ]
};

var gifLink = gifCollection.gifs;
var div = document.getElementById('#vids');

for (var i in gifLink) {
  var src = gifLink[i].gifLink;
  //alert(gifLink.gifLink);
  console.log(src);
  //div.innerHTML = div.innerHTML + '';
  $("#vids").append('

');
}

HTML


texts: Do CriteriaQuery code more readable and elegant

texts:
I wrote the code and would like to change it to more readable and elegant:

@Repository
public class ProductRepositoryImpl implements ProductCustomRepository {

private static final int MORE_THEN_RESULT = 1;

@PersistenceContext
private EntityManager em;

public List<Product> findByFilter(Filter filter) {
    final CriteriaBuilder cb = em.getCriteriaBuilder();
    final CriteriaQuery<Product> cq = cb.createQuery(Product.class);
    final Root<Product> from = cq.from(Product.class);

    try {
        final List<Predicate> predicates = createPredicatesByFilter(cb, from, filter);
        if (!CollectionUtils.isEmpty(predicates)) {
            cq.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
        }
    } catch (RuntimeException e) {
        return Collections.emptyList();
    }

    cq.select(from);

    return em.createQuery(cq).getResultList();
}

private List<Predicate> createPredicatesByFilter(CriteriaBuilder cb, Root<Product> from, Filter filter) {
    final List<Predicate> predicates = new ArrayList<>();

    if (!StringUtils.isEmpty(filter.getName())) {
        predicates.add(cb.like(cb.lower(from.<String>get("name")), filter.getName().toLowerCase() + "%"));
    }

    final BigDecimal priceBefore = filter.getPriceBefore();
    final BigDecimal priceAfter = filter.getPriceAfter();

    if (priceBefore != null && priceAfter != null) {
        if (priceBefore.compareTo(priceAfter) != MORE_THEN_RESULT) {
            predicates.add(cb.between(from.<BigDecimal>get("price"), priceBefore, priceAfter));
        } else {
            throw new RuntimeException();
        }
    } else if (priceBefore != null) {
        predicates.add(cb.gt(from.<BigDecimal>get("price"), priceBefore));
    } else if (priceAfter != null) {
        predicates.add(cb.lt(from.<BigDecimal>get("price"), priceAfter));
    }

    if (!StringUtils.isEmpty(filter.getCategory())) {
        predicates.add(cb.like(cb.lower(from.<Category>get("category").<String>get("name")), filter.getCategory().toLowerCase() + "%"));
    }

    return predicates;
}
}


texts:
I'm piecing together an Enemy script to attach to my characters. This is the portion that related the a game objects ability to patrol to given locations. Basically, patrolPoisitions are exactly what they say; they are defined in the editor. This is only the pertinent portion of the code, so there might be some straggler variables. I think I deleted them all though.

I am still very new to game development as well as Unity, so are there any suggestions on how to improve this code?

public float BaseAcceleration = 10.0f;
public float BaseSpeed = 5.0f;
public float IncreasedAccelerationFactor = 10.0f;
public float IncreasedSpeedFactor = 5.0f;
public float AlertCoolDownTime = 5.0f;
public float AlertDistance = 15.0f;
public float AttackDistance = 10.0f;
public float StoppingDistance = 0.0f;

private NavMeshAgent navMeshAgent = null;
public List<Vector3> patrolPositions = new List<Vector3>();
public float PatrolHoldTime = 0.0f;
private Vector3 StartPosition;
private int patrolPositionIndex = 0;
private bool isAtPatrolPosition = false;
private Vector3 CurrentDestination;

void Start () {
            navMeshAgent = GetComponent<NavMeshAgent> ();
            StartPosition = transform.position;
    }

void Update () {
            if (CurrentTarget) {
                    navMeshAgent.SetDestination (CurrentTarget.position);
            } else {
                    Patrol ();
            }

            SetNavMeshProperties ();
    }

#region Patrol Methods
void Patrol() {
            Vector3 Position = transform.position;
            if (patrolPositions != null && patrolPositions.Count > 1) {
                    Vector3 Destination = patrolPositions [patrolPositionIndex];
                    if (Destination != Position && CurrentDestination != Destination) {
                            Debug.Log (String.Format ("Patrol plotting route to {0}", Destination.ToString ()));
                            navMeshAgent.SetDestination (Destination);
                            CurrentDestination = Destination;
                            isAtPatrolPosition = false;
                    } else if (Destination == Position && !isAtPatrolPosition) {
                            isAtPatrolPosition = true;
                            Debug.Log (String.Format ("Patrol waiting {0} seconds", PatrolHoldTime));
                            StartCoroutine (Wait (PatrolHoldTime));
                    }
            } else if (Position != StartPosition) {
                    navMeshAgent.SetDestination (StartPosition);
            }
    }
void SetNavMeshProperties() {
            navMeshAgent.acceleration = BaseAcceleration;
            navMeshAgent.speed = BaseSpeed;
            navMeshAgent.stoppingDistance = StoppingDistance;
    }

IEnumerator Wait(float duration) {
            yield return new WaitForSeconds (duration);
            if (patrolPositionIndex < patrolPositions.Count - 1) {
                    patrolPositionIndex++;
            } else {
                    patrolPositionIndex = 0;
            }
            isAtPatrolPosition = false;
            Debug.Log (String.Format ("Patrol position index {0}", patrolPositionIndex.ToString ()));
    }
#endregion


texts: Backbone/requireJs and circular dependencies

texts:
I'm trying to have a main App view with references of all my views to manage and call transitions between theses views. But, I have circular dependencies.

app.js

define([
    'jquery',
    'underscore',
    'backbone',
    'views/viewOne'

], function ($, _, Backbone, ViewOne) {

    'use strict';
    var AppView = Backbone.View.extend({
        views:{},
        ref:'variable test',
        initialize: function () {

            this.views.viewOne = new myViewOne();

        }
    });

    return AppView;
});

views/viewOne.js

define([
    'jquery',
    'backbone',
    'views/app'
], function ($, Backbone, App) {

    'use strict';
    var ViewOne = Backbone.View.extend({({

        initialize: function () {
            App = require("views/app");
            console.log(App);
            //function (){ return parent.apply(this, arguments); }
        }

    });

    return ViewOne ;
});

I suppose require() sends me a string since the module was not called before.

Is an inline function a good approach in this case, and how can I work with it. I've tried this code to fix that, which seems to work.

app.js

this.views.viewOne = new myViewOne({parentView:this});

views/viewOne.js

var ViewOne = Backbone.View.extend({

    initialize: function (ref) {
        this.parentView = ref.parentView;
        console.log(this.parentView.ref);
        //variable test
    }
});

Is my approach correct?


texts: Replace any number of consecutive underscores with a single space

texts:
I am coding the following interesting problem :

  Remove '_' and print with 1 space from a given string
  For example, for "w_e_b__services" output should be : "w e b services".
  Multiple underscore should be substituted with only with one space.

The following is my code, which has some code duplication. I can see there are some ways to get rid of that duplication but that complicates the code in a different way. It would be great to get some code review on this.

public class ReplaceUnderscore {

    private static final char UNDERSCORE = '_';
    private static final char SPACE = ' ';

    public static String replace(String input) {

        if (input.isEmpty()) {
            return input;
        }

        StringBuilder output = new StringBuilder();

        char prevChar = input.charAt(0);
        if (prevChar == UNDERSCORE) {
            output.append(SPACE);
        } else {
            output.append(prevChar);
        }

        for (int i = 1; i < input.length(); i++) {
            char currentChar = input.charAt(i);

            if (currentChar == UNDERSCORE) {
                if (currentChar != prevChar) {
                    output.append(SPACE);
                }
            } else {
                output.append(currentChar);
            }

            prevChar = currentChar;
        }

        return output.toString();
    }
}

And here is the junit test:

public class StripUnderscoreTest {

    @Test
    public void strip() throws Exception {

        assertEquals(" ", ReplaceUnderscore.replace("_"));
        assertEquals("a b c", ReplaceUnderscore.replace("a_b_c"));
        assertEquals("a b c", ReplaceUnderscore.replace("a__b__c"));
        assertEquals("a b c", ReplaceUnderscore.replace("a___b___c"));
    }
}


texts:
how can I express this idea in Pythonic way?
The problem is that I want to create class with many attributes, but don't want to initialize all in __init__(self, arg1, arg2, ..., argN). Instead of this I want use some like this:

class DummyUpdater(object):

    def from_dict(self, d):
        self.__dict__.update(d)
        return self

    def __repr__(self):
        attributes = inspect.getmembers(self.__class__, lambda a: not(inspect.isroutine(a)))
        return '{}'.format({a[0]: getattr(self, a[0]) for a in attributes if not(a[0].startswith('__') and a[0].endswith('__'))})

class Passenger(DummyUpdater):
    """docstring for Passenger"""

    first_name = ''
    last_name = ''
    middle_name = ''
    title = ''
    maturity = ''
    document_number = ''
    document_type = ''

What is used now (with dummy updater):

passenger = Passenger().from_dict({'first_name' : 'Name', ...})

What I want to achive:

passenger = Passenger({'first_name' : 'Name', ...})
# and when I print passenger object I want see
print(passenger) # {'first_name' : 'Name', ...}


texts:
Kata: https://www.codewars.com/kata/linked-lists-sorted-insert

Description

  Write a SortedInsert() function which inserts a node into the correct location of a pre-sorted linked list which is sorted in ascending order. SortedInsert takes the head of a linked list and data used to create a node as arguments. SortedInsert() should also return the head of the list.

sortedInsert(1 -> 2 -> 3 -> null, 4) === 1 -> 2 -> 3 -> 4 -> null)
sortedInsert(1 -> 7 -> 8 -> null, 5) === 1 -> 5 -> 7 -> 8 -> null)
sortedInsert(3 -> 5 -> 9 -> null, 7) === 3 -> 5 -> 7 -> 9 -> null)

My Solution

def sorted_insert(head, data):
    prev = None
    node_j = head
    node_i = Node(data)
    while node_j:
        if node_j.data > data:
            node_i.next = node_j
            break
        prev = node_j
        node_j = node_j.next
    else:
        node_i.next = None
    if prev:
        prev.next = node_i
    return head if prev else node_i


texts: Methods to check form validity and to count invalid form fields

texts:
I have two these methods. Their loops are pretty similar. I'd like to refactor it to avoid duplicate nested loops. How could I do that?

@Override
public boolean isValid() {
    for (final FormSection formSection : getFormSections()) {
        for (final Component fieldComponent : formSection) {
            boolean isFieldValid = ((Field)fieldComponent).isValid();
            if(!isFieldValid){
                return false;
            }
        }
    }
    return true;
}

private int countErrors() {
    int errorAmount = 0;
    for (final FormSection formSection : getFormSections()) {
        for (final Component fieldComponent : formSection) {
            boolean isFieldValid = ((Field)fieldComponent).isValid();
            if(!isFieldValid){
                errorAmount++;
            }
        }
    }
    return errorAmount;
}

I've tried to use interface but it doesn't look good.

interface FieldHandler {
    boolean process(AtomicInteger number);
}

@Override
public boolean isValid() {
    return handleTextFieldsInTabs(new FieldHandler() {
        @Override
        public boolean process(AtomicInteger number) {
            return false;
        }
    }, null);
}

boolean handleTextFieldsInTabs(FieldHandler fieldHandler, AtomicInteger number) {
    for (final FormSection formSection : getFormSections()) {
        for (final Component fieldComponent : formSection) {
            boolean isFieldValid = ((Field)fieldComponent).isValid();
            if(!isFieldValid && !fieldHandler.process(number)) {
                return false;
            }
        }
    }
    return true;
}

private int countErrors() {
    final AtomicInteger errorAmount = new AtomicInteger(0);
    handleTextFieldsInTabs(new FieldHandler() {
        @Override
        public boolean process(AtomicInteger number) {
            number.incrementAndGet();
            return true;
        }
    }, errorAmount);
    return errorAmount.intValue();
}


texts: Concatenating two substrings to provide the largest possible palindromic string

texts:
The code extracts two substrings from different strings and concatinating them to provide the largest possible palindromic string.

Objective:

  You have two strings, (a) and (b). Find a string, (c), such that:
  (c)=(d)+(e).

  (d),(e) can be expressed as  where (d) is a non-empty substring of (a)
  and (e) is a non-empty substring of (b). (c) is a palindromic string.
  The length of is as long as possible. For each of the pairs of strings
  (a) and (b) received as input, find and print string  on a new line.
  If you're able to form more than one valid string , print whichever
  one comes first alphabetically. If there is no valid answer, print -1
  instead.

How can I improve the performance of this code, reducing the compile time and keeping the functionality the same?

import java.io.*;
import java.util.*;

public class Solution {
    boolean isPalindrome(String s) {
  int n = s.length();
  for (int i=0;i<(n / 2);++i) {
     if (s.charAt(i) != s.charAt(n - i - 1)) {
         return false;
     }
  }

  return true;
}

    public static void main(String[] args) {

        String result="";
         Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        for(int a=0; a<n; a++)
            {int length1, length2, i,c,d,j;
        int max_length=0;
        String string1 = in.next();
        String sub1,sub2;
        String string2 = in.next();
        length2=string2.length();
        length1 = string1.length();

      for( c = 0 ; c <length1 ; c++ )
      {
         for( i = length1-c ; i >0 ; i-- )
         {
            sub1 = string1.substring(c, c+i);
            for( d = 0 ; d < length2 ; d++ )
      {
         for( j = length2-d ; j >0 ; j-- )
         {
            sub2 = string2.substring(d, d+j);
            String temp=sub1+sub2;
              Solution obj= new Solution();
             if(temp.length()>=max_length && obj.isPalindrome(temp)==true)

                 {
                 if (max_length==temp.length())
                  {   if(temp.compareTo(result)<0)
                     {
                     result=temp;
                  }}
                 else {
                     max_length=temp.length();
                 result=temp;
                  }
             }
         }
      }
         }
      }
             if(max_length==0)
                 System.out.println(-1);
             else
                 {
       System.out.println(result);
             result="";
             }
        }    /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    }
}


texts:
It is a set of edges of a graph given. An edge is given as an array of its endpoints, e.g. [:a, :b]. The array can contain an arbitrary amount of endpoints. Therefore [] and [:b, :c, :d] are also valid edges.

I want to find all islands which means that I looking for unconnected sets of nodes. Here is my Ruby solution.

e = [[:a, :b],[:c, :d],[:b, :c, :e], [:f, :g]]
n = e.flatten.uniq

def islands(edges)
  edges.reduce([]) do |islands, edge|
    matches = islands.select { |island| island.any? { |node| edge.include?(node) } }

    if !matches.empty?
      island = matches.shift
      islands.delete_if { |i| matches.include?(i) }

      island.concat(matches.flatten)
      island.concat(edge)
      island.uniq!
    else
      islands << edge
    end

    islands
  end
end

puts islands(e).inspect
# => [[:a, :b, :c, :d, :e], [:f, :g]]

Is there room for improvement?


texts:
Here is my controller class for my MVC pattern with all the includes removed.  From what I know of the MVC pattern it is good to have a small controller class.  Mine is just a collection of public static functions which accept requests and send data to the client.

Regarding send().  I'm just wrapping the echo command for sending text from an Ajax call.  This is for consistency and to have all text exiting the same point in code.

I have a ControlEntry (Snippet 2) which help decouple Super Globals and direct entry into the Control, so the two kind of work in Tandem.

Pull_Pic just pulls a path and inserts it into HTML for display.

Snippet 1

<?php

    class Control
    {
        public static function ajax($ajax_type)
        {
            Session::start();
            switch($ajax_type)
            {
                case 'ControlBookmark_add':
                    $control_bookmark_instance = new ControlBookmark('remove');
                    $control_bookmark_instance->add();
                    break;
                case 'ControlBookmark_delete':
                    $control_bookmark_instance = new ControlBookmark('remove');
                    $control_bookmark_instance->delete();
                    break;
                case 'ControlTweet_add':
                    $control_tweet_instance = new ControlTweet('remove');
                    $control_tweet_instance->add();
                    break;
                case 'ControlSignIn':
                    $control_signin_instance = new ControlSignIn('remove');
                    $control_signin_instance->invoke();
                    break;
                case 'ControlSignUp':
                    new ControlSignUp();
                    break;
                case 'ControlTryIt':
                    new ControlTryIt();
                    break;
                case 'ControlSignOut':
                    Session::finish();
                    new ControlSignOut();
                    break;
                default:
                    throw new Exception('Invalid ajax_type');
            }
        }
        public static function reload() // Because reload may be triggered by File Upload (second control path)
        {
            if (session_id() == "")
            {
                Session::start();
            }
            if((Session::status())==='bookmarks')
            {
                include 'class.ViewBookmarks.php';
            }
            else
            {
                include 'class.ViewMain.php';
            }
        }
        public static function upload($fileType, $fileName)
        {
            Session::start();
            $fileInstance = new UploadedFile($fileType, $fileName);
            $fileInstance->createImages();
            Session::updatePic();
            self::reload();
        }
        public static function send($textStream)
        {
            echo $textStream;
        }
    }

**Snippet 2**

    new ControlEntry();

    class ControlEntry
    {
        public function __construct()
        {
            if(isset( $_FILES['ufile']['tmp_name']) )
            {
                if( ( $_FILES['ufile']['tmp_name']) != '' )
                    {
                    Control::upload( $_FILES['ufile']['type'], $_FILES['ufile']['tmp_name'] );
                    }
                else
                    {
                    Control::reload();
                    }
            }
            else if( isset($_POST['ajax_type']) )
            {
                Control::ajax( $_POST['ajax_type'] );
            }
            else
            {
                Control::reload();
            }
        }
    }


texts: Finding the most frequent words in Pandas dataframe

texts:
I am new in Python coding. I think the code could be written in a better and more compact form. It compiles quite slowly due to the method of removing stop-words.
I wanted to find the top 10 most frequent words from the column excluding the URL links, special characters, punctuations... and stop-words.
Any criticisms and suggestions to improve the efficiency & readability of my code would be greatly appreciated. Also, I want to know if there exists any dedicated python module to get the desired result easily.
I have a dataframe df such that:
print(df['text'])

0         If I smelled the scent of hand sanitizers toda...
1         Hey @Yankees @YankeesPR and @MLB - wouldn't it...
2         @diane3443 @wdunlap @realDonaldTrump Trump nev...
3         @brookbanktv The one gift #COVID19 has give me...
4         25 July : Media Bulletin on Novel #CoronaVirus...
                                ...
179103    Thanks @IamOhmai for nominating me for the @WH...
179104    2020! The year of insanity! Lol! #COVID19 http...
179105    @CTVNews A powerful painting by Juan Lucena. I...
179106    More than 1,200 students test positive for #CO...
179107    I stop when I see a Stop\n\n@SABCNews\n@Izinda...
Name: text, Length: 179108, dtype: object

I have done it in the following way:
import pandas as pd
import nltk
import re
import string
from nltk.corpus import stopwords
nltk.download('punkt')
nltk.download('stopwords')
from nltk.tokenize import word_tokenize

stop_words = stopwords.words()

def cleaning(text):
    # converting to lowercase, removing URL links, special characters, punctuations...
    text = text.lower()
    text = re.sub('https?://\S+|www\.\S+', '', text)
    text = re.sub('<.*?>+', '', text)
    text = re.sub('[%s]' % re.escape(string.punctuation), '', text)
    text = re.sub('\n', '', text)
    text = re.sub('[’“”…]', '', text)

    # removing the emojies               # https://www.kaggle.com/alankritamishra/covid-19-tweet-sentiment-analysis#Sentiment-analysis
    emoji_pattern = re.compile("["
                           u"\U0001F600-\U0001F64F"  # emoticons
                           u"\U0001F300-\U0001F5FF"  # symbols & pictographs
                           u"\U0001F680-\U0001F6FF"  # transport & map symbols
                           u"\U0001F1E0-\U0001F1FF"  # flags (iOS)
                           u"\U00002702-\U000027B0"
                           u"\U000024C2-\U0001F251"
                           "]+", flags=re.UNICODE)
    text = emoji_pattern.sub(r'', text)

    # removing the stop-words
    text_tokens = word_tokenize(text)
    tokens_without_sw = [word for word in text_tokens if not word in stop_words]
    filtered_sentence = (" ").join(tokens_without_sw)
    text = filtered_sentence

    return text

dt = df['text'].apply(cleaning)

from collections import Counter
p = Counter(" ".join(dt).split()).most_common(10)
rslt = pd.DataFrame(p, columns=['Word', 'Frequency'])
print(rslt)

          Word  Frequency
0      covid19     104546
1        cases      18150
2          new      14585
3  coronavirus      14189
4          amp      12227
5       people       9079
6     pandemic       7944
7           us       7223
8       deaths       7088
9       health       5231

An example IO of my function cleaning():
inp = 'If I smelled the scent of hand sanitizers today on someone in the past, I would think they were so intoxicated that… https://t.co/QZvYbrOgb0'
outp = cleaning(inp)
print('Input:\n', inp)
print('Output:\n', outp)

Input:
 If I smelled the scent of hand sanitizers today on someone in the past, I would think they were so intoxicated that… https://t.co/QZvYbrOgb0
Output:
 smelled scent hand sanitizers today someone past would think intoxicated


texts:
I have created a class which wraps pointers and behaves like a pointer would behave, minus a few cases. It is a lock wrapper class which simply locks before usage of the pointer and unlocks once the pointer usage is done.

It must behave exactly like a pointer EXCEPT in the following cases:

It cannot be implicitly converted to the pointer type it is pointing to
It does not handle any implicit memory allocation/deallocation (it provides a method to delete the pointer to the object)
When compared to another wrapper object, it compares their pointers to see if they are the same

Here is what I have so far:

template <class PtrType, class LockType>
class ELockWrapper {
public:
    class Proxy {
    public:
        Proxy(PtrType* p, LockType* lock) : ptr(p), mLock(lock)  { mLock->Lock(); }
        ~Proxy()                                                 { mLock->Unlock(); }
        PtrType* operator->()                                    { return ptr; }
        PtrType  operator*()                                     { return *ptr; }
    private:
        PtrType*    ptr;
        LockType*   mLock;
    };

    ELockWrapper()                            : ptr(nullptr), lock(nullptr) {}
    ELockWrapper(nullptr_t t)                 : ELockWrapper()   {}
    ELockWrapper(PtrType *p, LockType* l)     : ptr(p), lock(l)  {}
    ELockWrapper(PtrType *p, LockType& l)     : ptr(p), lock(&l) {}
    ELockWrapper(const ELockWrapper& copy) = default;

    ELockWrapper& operator=(const ELockWrapper& x) = default;
    bool operator==(const ELockWrapper& cmp) { return cmp.ptr == ptr; }
    bool operator!=(const ELockWrapper& cmp) { return !operator==(cmp); }
    bool operator==(PtrType* t)              { return ptr == t; }
    bool operator!=(PtrType* t)              { return ptr != t; }
    bool operator==(bool b)                  { return (ptr && b) || (!ptr && !b); }
    bool operator!=(bool b)                  { return !operator==(b); }
    operator bool() const                    { return ptr; }

    Proxy operator->() {
        return Proxy(ptr, lock);
    }

    PtrType operator*() {
        return *Proxy(ptr, lock);
    }

    void Delete() {
        Proxy(ptr, lock);
        delete ptr;
    }

private:
    PtrType*    ptr;
    LockType*   lock;
};

Here is it in action with some test cases.

Any mistakes/suggestions would be much appreciated.

One quick thing I want to ask: if ANY of the methods on ELockWrapper can be called concurrently, should I wrap each overloaded boolean operator with a lock? I'm thinking perhaps the delete method will be called, which a thread was interrupted in one of the operators might be problematic. Just a confirmation if this is the right thing to do?


texts: Counting pairs of elements in a circular ArrayList containing -1, 0, and 1

texts:
I have the following method:

public void test() {

    List<Integer> slopeChanges = new ArrayList<Integer>();
    slopeChanges.add(0);
    slopeChanges.add(0);
    slopeChanges.add(1);
    slopeChanges.add(0);
    slopeChanges.add(1);

    List<Double> sixProbabilites = new ArrayList<Double>();

    int count_neg10 = 0, count_neg11 = 0, count_10 = 0, count_1neg1 = 0, count_0neg1 = 0, count_01 = 0;

    for(int i=0; i<slopeChanges.size(); i++) {
        if(i+1 != slopeChanges.size()) {
            if((slopeChanges.get(i) == -1) && (slopeChanges.get(i+1) == 0)) {count_neg10++;}
            else if((slopeChanges.get(i) == -1) && (slopeChanges.get(i+1) == 1)) {count_neg11++;}
            else if((slopeChanges.get(i) == 1) && (slopeChanges.get(i+1) == 0)) {count_10++;}
            else if((slopeChanges.get(i) == 1) && (slopeChanges.get(i+1) == -1)) {count_1neg1++;}
            else if((slopeChanges.get(i) == 0) && (slopeChanges.get(i+1) == -1)) {count_0neg1++;}
            else if((slopeChanges.get(i) == 0) && (slopeChanges.get(i+1) == 1)) {count_01++;}
        }else {
            if((slopeChanges.get(i) == -1) && (slopeChanges.get(0) == 0)) {count_neg10++;}
            else if((slopeChanges.get(i) == -1) && (slopeChanges.get(0) == 1)) {count_neg11++;}
            else if((slopeChanges.get(i) == 1) && (slopeChanges.get(0) == 0)) {count_10++;}
            else if((slopeChanges.get(i) == 1) && (slopeChanges.get(0) == -1)) {count_1neg1++;}
            else if((slopeChanges.get(i) == 0) && (slopeChanges.get(0) == -1)) {count_0neg1++;}
            else if((slopeChanges.get(i) == 0) && (slopeChanges.get(0) == 1)) {count_01++;}
        }
    }

    System.out.println(slopeChanges);

    if(count_neg10 != 0) {System.out.println("count_neg10 = " + count_neg10);}
    if(count_neg11 != 0) {System.out.println("count_neg11 = " + count_neg11);}
    if(count_10 != 0)    {System.out.println("count_10 = " + count_10);}
    if(count_1neg1 != 0) {System.out.println("count_1neg1 = " + count_1neg1);}
    if(count_0neg1 != 0) {System.out.println("count_0neg1 = " + count_0neg1);}
    if(count_01 != 0)    {System.out.println("count_01 = " + count_01);}

    if(count_neg10 != 0) {sixProbabilites.add((double)count_neg10 / ((double)slopeChanges.size()));}
    if(count_neg11 != 0) {sixProbabilites.add((double)count_neg11 / ((double)slopeChanges.size()));}
    if(count_10 != 0) {sixProbabilites.add((double)count_10 / ((double)slopeChanges.size()));}
    if(count_1neg1 != 0) {sixProbabilites.add((double)count_1neg1 / ((double)slopeChanges.size()));}
    if(count_0neg1 != 0) {sixProbabilites.add((double)count_0neg1 / ((double)slopeChanges.size()));}
    if(count_01 != 0) {sixProbabilites.add((double)count_01 / ((double)slopeChanges.size()));}
}

The slopeChanges list contains symbols that are either 0, 1, or -1. I would like to count frequencies of sub-blocks that are either 01, 10, -11, 1-1, -10, 0-1. The method that I showed does what I want, for example, this block of symbols 00101 should result in count_10 = 2 which means 10 sub-block occurs twice while 01 (i.e., count_01 = 2) occurs twice. Please note that the we consider the first symbol is also last.

As you can see, there are lot of duplication in the code in order to perform what I want. Is there a better way to re-write this function in a very elegant style?


texts: Generate XKCD passwords from single Bash line

texts:
There are a few "XKCD password" generators out there, but the task seems so straight forward, so I wanted a compact and simple solution. My requirements were:

One-line alias (or few-line function) to put in .profile
Standard Unix commands (require no packages)
Column output (like pwgen)
Titlecase words (for readability)
True randomness

(I'm happy to renounce command line options in exchange for simplicity.)

for i in {1..32}; do grep -E "^[a-z]{4,8}$" /usr/share/dict/words | gshuf -n4 | gsed 's/.*/\u&/' | tr -d '\n' | awk '{print $1}'; done | column

Current problems:

shuf requires brew install coreutils (since I'm on macOS).
sed requires brew install gnu-sed.
tr -d '\n' | awk '{print $1}' seems silly.
shuf: Is it truly random?

How can I make a short, yet readable the command line that works on "all" Unix flavours?

PS! I also found that by using SCOWL, which has a range from short word lists with common names to long word lists with obscure words, you can trade-off memorability Vs. entropy.


texts: Using Support Vector Machine algorithm to analyze satellite image

texts:
I am using Support Vector Machine (SVM) algorithm to perform a classification. The satellite image I am using is really big (5GB) that's why I am trying to take advantage of multiprocessing tool to speed up the process.

My problem is that my PC does not use all the available cores. I run the code to my laptop (4 cores) but it takes for ever for the process to finish. It uses all the 4 CPU cores though. When I try to run the same code on the desktop PC which has 12 CPU cores, only 5 of them reaches 100%.
below, we see the image to be classified and the training data that are used on the right

import os
import numpy as np
from osgeo import gdal, gdal_array, gdalconst
from osgeo import ogr
import pandas as pd
import image_slicer

from sklearn.svm import SVC
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score
from sklearn.model_selection import cross_val_score, StratifiedKFold, StratifiedShuffleSplit
from sklearn.model_selection import validation_curve, GridSearchCV
from sklearn.pipeline import Pipeline

from matplotlib.pylab import *
from multiprocessing import Pool
import time

img = 'sea_ice.tif' #image to be used for classification, 3000 x 3000 pixels
roi = 'training_data.shp' #training data

X = img[roi_int > 0, 2:] #X is the matrix containing our features
y = roi[roi>0] #y contains the values of our training data

#Split our dataset into training and testing. Test data will be used to make predictions
split_test_data = 0.30
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=split_test_data, random_state=42)
stratified = StratifiedShuffleSplit(len(y_train), 10, split_test_data, random_state=0)

#use pipeline method to do all the steps automatically
pip = Pipeline([ ('scale', MinMaxScaler()),
             ('svm', SVC())])
pip.fit(X_train, y_train)

def predict(input_data):
    img_predict = pip.predict(input_data)
    return img_predict

start = time.time()

tfs_shape = (img.shape[0] * img.shape[1], img.shape[2]-2 )
tfs_2D = img[:, :, 2:].reshape(tfs_shape)

# split good data into chunks for parallel processing
tfsChunks = np.copy(tfs_2D)
split = np.array_split(tfsChunks, 9)

# run parallel processing of all data with SVM
pool = Pool(9)
svmLablesGood = pool.map(predict, split)

# join results back from the queue and insert into full matrix
svmLabelsGood = np.hstack(svmLablesGood)

# reshape labels from vector into 2D raster map
svm_reshape = svmLabelsGood.reshape(img.shape[0], img.shape[1])
plt.imshow(svm_reshape)
plt.show()

end = time.time()
print 'the processing time is:', end - start, '\n'

#Evaluate the model using K-fold cross-validation
scores_svm = cross_val_score(pip, X_test, y_test, scoring='accuracy', cv=stratified)
print ('Accuracy: %0.2f (+/- %0.2f)' % (scores_svm.mean(), scores_svm.std())), '\n'
print ('10 parts cross-validation:' , scores_svm), '\n'

score_train = pip.score(X_train,y_train)
score_test = pip.score(X_test,y_test)
print 'training score:', score_train, '\n'
print 'testing score:', score_test, '\n'

below, the results from the classification are shown (after 3 days of processing). I need to classify areas with ice and ice free areas. Yellow areas show the ice.

I cannot wait for 3 days for my laptop to finish the process. I want to take advantage of the 12 CPU cores of the desktop PC to speed up the process, but I do not understand why only 5 cores are used.


texts: Robustly dealing with malformed Unicode files

texts:
I'm writing a script that deals reads UTF-8-encoded XML files and writes parts of those files into a tempfile for further processing.

Sometimes, the input files will have a few malformed characters. If it's just "a few" occurrences, I want those glossed over, but if the file is majorly corrupted, I want to see an error message. I have a solution for this working right now, but I have this nagging feeling that this isn't a clean way to do  this.

This creates an example file with malformed data:

f = open("test.xml","w+b")
f.write("\x3c\x61\x3e\x31\x3c\x2f\x61\x3e\x0a\x3c\x62\x3e\x32\xc3\x0a\xbc\x3c\x2f\x62\x3e\x0a")
f.close()

Yes, I know it's probably a stray linebreak that's causing my "malformed utf-8" problem, but let's not make that the issue here.

Now, the following is a simplified version I have in place to deal with malformed utf-8 data up to a certain limit:

import codecs

sourcefile = open("test.xml", "r")

# First line always XML declaration, no special treatment
line = sourcefile.readline()

i = 1
unicodeerror = 0

while line:
    try:
        pos = sourcefile.tell()
        line = sourcefile.readline().decode("utf-8")
    except UnicodeDecodeError as e:
        print "WARNING: Encoding error in line " + str(i+1)
        sourcefile.seek(pos, 0)
        line = sourcefile.readline().decode("utf-8","ignore")
        unicodeerror += 1
    i += 1

if unicodeerror > 5:
    raise StandardError('ERROR: Too many Unicode errors')

As you can see, I read the input file as a byte string, and then read each line individually, doing the UTF-8-Decode within the try-except-clause. That way, I can catch the UnicodeDecodeError and count it. Only after counting it, reread the line, this time ignoring possible mistakes.

However, it "feels wrong" to simply read the file like this instead of doing a codecs.open(FILE, MODE, "utf-8"). With the codecs method, however, I see no way to reread the line with a different error handling directive after I hit the UnicodeDecodeException.

Am I being too cautious? Is this maybe the best / only reliable way to do this? OR is there a much easier solution (which can be achieved within the standard library!)?


texts: Exporting Shellbags, Jump Lists, and LNK files with PowerShell

texts:
I'm new to PowerShell (started yesterday) and was wondering if there is a way of exporting Shell bags, Jump lists, and LNK files from the Reg to a local computer's desktop using PowerShell? I have tried a few things and can export them as CSVs... but I'm not sure if that's going to have any forensic merit (dabbling in forensics for some fun but only just started).
Here is my code below for how I am doing it... but I was wondering if there is a more elegant or 'proper' way of doing it? I haven't used variables or anything yet because I'm not that far into tutorials yet.
Any tips or suggestions on making this more dynamic/robust/efficient would be greatly appreciated too!
##Create parent folder for storing retrieved artifacts
$DesktopPath = [Environment]::GetFolderPath([System.Environment+SpecialFolder]::Desktop)
New-Item -Path "$DesktopPath\Forensics" -ItemType Directory
$parentFolder = "$DesktopPath\Forensics"

##BEGIN SCRIPT:
Write-Host "Beginning extraction"

##SHELLBAGS
Write-Host "Generating ShellBag CSVs"
Function ShellBags {
New-Item -Path C:\Users\User\Desktop\Forensics\Shellbags -ItemType Directory
Get-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Local Settings\Software\Microsoft\Windows\Shell\BagMRU' |
    Export-Csv -Path "$parentFolder\Shellbags\Shellbags_MRU.csv" -UseCulture -NoTypeInformation
Get-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Local Settings\Software\Microsoft\Windows\Shell\Bags' |
     Export-Csv -Path "$parentFolder\Shellbags\Shellbags.csv" -UseCulture -NoTypeInformation
Get-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Local Settings\Software\Microsoft\Windows\Shell\MuiCache'|
    Export-Csv -Path "$parentFolder\Shellbags\Shellbags_MUIcache.csv" -UseCulture -NoTypeInformation
}
ShellBags

##JUMPLISTS:
Write-Host "Generating JumpLists CSV"
Function JumpLists {
New-Item -Path .\USERS\USER\DESKTOP\Forensics\JumpLists -ItemType Directory
Get-ItemProperty -Path 'C:\Users\*\AppData\Roaming\Microsoft\Windows\Recent\AutomaticDestinations' |
    Export-Csv -Path "$parentFolder\JumpLists\JumpLists.csv" -UseCulture -NoTypeInformation
}
JumpLists

##LNKLISTS:
Write-Host "Generating LnkLists CSV"
Function LNKList {
New-Item -Path .\Users\User\Desktop\Forensics\LNKList -ItemType Directory
Get-ItemProperty -Path C:\Users\*\AppData\Roaming\Microsoft\Windows\Recent\|
     Export-Csv -Path "$parentFolder\LNKList\LNKList_Windows.csv" -UseCulture -NoTypeInformation
Get-ItemProperty -Path C:\Users\*\AppData\Roaming\Microsoft\Office\Recent\ |
     Export-Csv -Path "$parentFolder\LNKList\LNKList_Office.csv" -UseCulture -NoTypeInformation
}
LNKList

##LASTLOGON:
Write-Host "Generating LastLogon CSV"
Function LastLogon {
New-Item -Path .\Users\User\Desktop\Forensics\LastLogon -ItemType Directory
Get-LocalUser | Select * | Export-Csv -Path "$parentFolder\LastLogon\LastLogon.csv" -UseCulture -NoTypeInformation
}
LastLogon


texts:
I am trying to learn some Java on my own, and I am tackling some "programming challenge" problems to practice the little I have learnt so far.

Could anybody constructively critique this beginner's effort (source on Bitbucket)?

public class BinaryCode {
  public String[] decode(String message) {
    int messageLength = message.length();
    return new String[] { decodeMessage(message, messageLength, 0),
        decodeMessage(message, messageLength, 1) };
  }
  private String decodeMessage(String encodedMessage, int messageLength,
      int assumedFirstDigit) {
    StringBuilder decodedMessage = new StringBuilder(messageLength);
    int nextDecodedDigit = assumedFirstDigit;
    for (int i = 0; i < messageLength; i++) {
      if (nextDecodedDigit != 0 && nextDecodedDigit != 1) {
        return "NONE";
      }
      decodedMessage.append(nextDecodedDigit);
      int encodedDigit = encodedMessage.charAt(i) - '0';
      int decodedDigit = decodedMessage.charAt(i) - '0';
      nextDecodedDigit = encodedDigit - decodedDigit;
      if (i > 0) {
        nextDecodedDigit -= decodedMessage.charAt(i - 1) - '0';
      }
    }
    int checkValue = decodedMessage.charAt(messageLength - 1) - '0';
    if (messageLength > 1) {
      checkValue += decodedMessage.charAt(messageLength - 2) - '0';
    }
    if (checkValue == encodedMessage.charAt(messageLength - 1) - '0') {
      return decodedMessage.toString();
    }
    return "NONE";
  }
}


texts: Iterate over a list of list names as file names

texts:
Interrogating a web API using query-url's, for each query I can get either zero hits, one hit, or multiple hits. Each of those categories needs to go into a separate CSV file for manual review or processing later. (More on this project here and here).

The input data (from a 14K line csv, one line per artist) is full of holes. Only a name is always given, but may be misspelled or in a form that the API does not recognise. Birth dates, death dates may or may not be known, with a precision like for example 'not before may 1533, not after january 1534'. It may also have exact dates in ISO format.

Using those three different output csv's, the user may go back to their source, try to refine their data, and run the script again to get a better match. Exactly one hit is what we go for: a persistent identifier for this specific artist.

In the code below, df is a Pandas dataframe that has all the information in a form that is easiest to interrogate the API with.

First, I try to get an exact match best_q (exact match of name string + any of the available input fields elsewhere in the record), if that yields zero, I try a slightly more loose match bracket_q (any of the words in the literal name string + any of the available input fields elsewhere in the record).

I output the dataframe as a separate csv, and each list of zero hits, single hits, or multiple hits also in a separate csv.

I'm seeking advice on two specific things.

Is there a more Pythonic way of handling the lists? Right now, I think the code is readable enough, but I have one line to generate the lists, another to put them in a list of lists, and another to put them in a list of listnames.
The second thing is the nested if..elif on zero hits for the first query. I know it ain't pretty, but it's still quite readable (to me), and I don't see how I could do that any other way. That is: I have to try best q first, and only if it yields zero, try again with bracket_q.

I have omitted what goes before. It works, it's been reviewed, I'm happy with it.

A final note: I'm not very concerned about performance, because the API is the bottleneck. I am concerned about readability. Users may want to tweak the script, somewhere down the line.

singles, multiples, zeroes = ([] for i in range(3))

for row in df.itertuples():
    query = best_q(row)
    hits, uri = ask_rkd(query)
    if hits == 1:
        singles.append([row.priref, row.name, hits, uri])
    elif hits > 1:
        multiples.append([row.priref, row.name, hits])
    elif hits == 0:
        query = bracket_q(row)
        hits, uri = ask_rkd(query)
        if hits == 1:
            singles.append([row.priref, row.name, hits, uri])
        elif hits > 1:
            multiples.append([row.priref, row.name, hits])
        elif hits == 0:
            zeroes.append([row.priref, str(row.name)])  # PM: str!!

lists = singles, multiples, zeroes
listnames = ['singles','multiples','zeroes']

for s, l in zip(listnames, lists):
    listfile = '{}_{}.csv'.format(input_fname, s)
    writelist(list=l, fname=listfile)

outfile = fname + '_out' + ext
df.to_csv(outfile, sep='|', encoding='utf-8-sig')


texts: Print all possible codes for a given string

texts:
My question is to print all possible codes for the string.

Assume that value of a=1, b=2, c=3, d=4, .... z=26.

E.g. for “1123” possible codes are aabc, kbc, alc, aaw, kw

Here's what I could come up with.

   private static void printCodes(String str, String ans) {
    if (str.length() == 0) {
        System.out.println(ans);
        return;
    }

    String ch1 = str.substring(0, 1);
    String restOfTheString1 = str.substring(1);

    if (Integer.parseInt(ch1, 10) > 0) {
        char code = (char) (Integer.parseInt(ch1, 10) + 'a' - 1);
        printCodes(restOfTheString1, ans + code);
    }

    if (str.length() >= 2) {
        String ch12 = str.substring(0, 2);
        String ros2 = str.substring(2);

        if (Integer.parseInt(ch12, 10) > 0 && Integer.parseInt(ch12) <= 26) {
            char code = (char) (Integer.parseInt(ch12, 10) + 'a' - 1);
            printCodes(ros2, ans + code);
        }
        if (Integer.parseInt(ch12, 10) == 0) {
            printCodes(ros2, ans);
        }
    }
}

Let me know if I did something wrong or anything could have been better.
Thanks!


texts: PowerShell - fast remove a directory with 10,000+ files

texts:
I am sick of File Explorer's super slow deletion speed, so I tried to write a PowerShell script to make deletion faster, and while it does its job, its speed isn't as high as what I intended it to be.
I have written this:
Function Fast-Delete {
    Param(
    [Parameter(Valuefrompipeline=$True, Mandatory=$True)] [String]$Directory0
    )
    Write-Warning "This Process Will Delete Everything In The Target Directory: $Directory0, Do You Want To Confirm Deletion?" -Warningaction Inquire
    $Directory=$Directory0
    While ((Get-Childitem -Path $Directory0 -Depth 0 -Force).Count -Ne 0) {
    If ((Get-Childitem -Path $Directory -Directory -Force -Depth 0).Count -Ne 0) {
    $Directory=(Get-Childitem -Path $Directory -Directory -Force -Depth 0).Fullname | Select-Object -Index 0
    }
    If ((Get-Childitem -Path $Directory -File -Force).Count -Ne 0) {
    (Get-Childitem -Path $Directory -File -Recurse -Force).Fullname | Foreach {Remove-Item -Path $_}
    }
    $Directory1=$Directory
    $Directory=$Directory | Split-Path -Parent
    Remove-Item -Path $Directory1
    }
    Remove-Item -Path $Directory0
}

It is significantly faster than explorer, but still isn't ideal, I have tested it, I used it to delete 208,000 files in 1,000 folders , and the folders disappear at speed of 1 per second, so it's about 208 files/s, now the next challenge should be parallelization, But this is currently really way above me, but it shouldn't be too hard, I am just not experienced enough.
Update2
I have managed to make it run in parallel with this script:
function Parallel-Delete {
    param(
    [Parameter(Valuefrompipeline=$true, Mandatory=$true, Position=0)] [array]$filelist,
    [Parameter(Valuefrompipeline=$true, Mandatory=$true, Position=1)] [int]$number
    )
    0..($filelist.count-1) | Where-Object {$_ % 16 -eq $number} | foreach {Remove-Item -Path $filelist[$_]}
}

Function Fast-Delete {
    Param(
    [Parameter(Valuefrompipeline=$True, Mandatory=$True)] [String]$Directory0
    )
    Write-Warning "This Process Will Delete Everything In The Target Directory:  $Directory0, Do You Want To Confirm Deletion?" -Warningaction Inquire
    $Directory=$Directory0
    While ((Get-Childitem -Path $Directory0 -Depth 0 -Force).Count -Ne 0) {
        If ((Get-Childitem -Path $Directory -Directory -Force -Depth 0).Count -Ne 0) {
            $Directory=(Get-Childitem -Path $Directory -Directory -Force -Depth 0).Fullname | Select-Object -Index 0
        }
        If ((Get-Childitem -Path $Directory -File -Force).Count -Ne 0) {
            If ((Get-Childitem -Path $Directory -File -Force).Count -Ge 128) {
                [array]$filelist=(Get-Childitem -Path $Directory -File -Force).Fullname
                0..15 | foreach-object {Invoke-Command -ScriptBlock { Parallel-Delete $filelist $_}}
                } else {
                (Get-Childitem -Path $Directory -File -Force).Fullname | Foreach {Remove-Item -Path $_}
            }
        }
        $Directory1=$Directory
        $Directory=$Directory | Split-Path -Parent
        Remove-Item -Path $Directory1
        }
    Remove-Item -Path $Directory0 -erroraction silentlycontinue
}
$Directory0=Read-Host "Please input target directory to be deleted"
Fast-Delete $Directory0

But it still isn't ideal, it isn't 15 times faster as expected, what did I miss?
Edit: simplified the creation of parallel processes.


texts: Word Separator and Pig Latin Program Final Edit

texts:
This is an updated version of my previous Java program that asks the user for a sentence and then separates the words and then converts the sentence into Pig Latin. My previous program was working, but there was some tweeking that needed to be done which I did after getting positive feedback. Apparently, my loop in the separateWords function was checking for capital letters twice or so. I have included the instructions for the assignment for reference and the code for peer edit. Thank you.

The program will accept a string as input in which all of the words are run together, but the first character of each word is uppercase. Convert the string to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string "StopAndSmellTheRose" would be converted to "Stop and smell the roses"

Then the program will convert each word in the result string of task 1 into "Pig Latin". In one version of Pig Latin, you convert a word by removing the first letter, placing that letter at the end of the word, and then appending "ay" to the word.

For example, for the result string "Stop and smell the roses" in task 1, the Pig Latin string should be "topSay ndaay mellsay hetay osesray"

Requirements:

Your program should have 3 methods: a main method, a method for task 1, and a method for task2

the method for task 1 should return the result string of task1
the method for task 2 should display the Pig Latin string

public class Assignment9 {
public static void main(String[] args) {
    // Variables
    String sentence, revisedSentence;

    // Create a Scanner object for keyboard input
    Scanner keyboard = new Scanner(System.in);

    // Get the input string
    System.out.print("Enter sentence: ");
    sentence = keyboard.nextLine();

    //Close keyboard
    keyboard.close();

    // Call function to perform Task 1
    revisedSentence = separateWords(sentence);

    System.out.print("Revised Sentence: " +revisedSentence);

    // Call function to perform Task 2
    toPigLatin(revisedSentence);
}

private static String separateWords(String sentence)
{
    // Variables
    StringBuilder str = new StringBuilder(sentence);
    int i = 1;

     // While loop repeats until the end of the sentence
    while (i < str.length())
    {
        // Checks for upper case characters
        if(Character.isUpperCase(str.charAt(i)))
        {
            str.insert(i, ' ');
            i++;
            char ch = Character.toLowerCase(str.charAt(i));
            str.setCharAt(i, ch);
        }
        i++;
    }
    return str.toString();
}

private static void toPigLatin(String revisedSentence)
{
    // Variables
    String latin = " ";

    // Split sentence by spaces
    String[] tokens = revisedSentence.split(" ");

    // Convert English sentence into Pig Latin
    for (int i = 0; i < tokens.length; i++)
    {
        // Get string from array
        String str = tokens[i];

        // Get first letter from string
        String str1 = str.substring(0, 1);

        // Get substring from string
        String str2 = str.substring(1, str.length());

        // Concatenate the two strings in a required format
        str2 = str2.concat(str1);

        // Concatenate the result and "AY"
        str2 = str2.concat("ay");

        // Make a sentence with all the words
        latin = latin.concat(str2 + " ");
    }

    // Display pig latin verison
    System.out.println("\nPig Latin Version:" +latin );
    }
}


texts:
To learn Vue.js I have made a tree viewer with the code below. Can anyone give me some advice on how to improve the code?

  <tree v-bind:nodes="nodes"></tree>


texts:
Motivation

I recently picked up Emacs. As part of the process, I've started to configure the editor to fit my needs with Emacs Lisp. The Elisp indentation rules are still foreign to me, so I pretty much depend on Emacs' electric-indent-mode to work. There's always C-x h and C-M-\ to the rescue, though.

That being said, I recently came across a user who cannot use this functionality, as the Emacs user interface in its default state is apparently incompatible with their screen reader. Still, I think that Emacs' Lisp indentation works great, so I tried to make it more accessible. And since I also needed some experience with Lisp, why not write it in Elisp?

Emacs driven indentation script

The Emacs driven indentation script (Edis) is single script to indent files with Emacs' indentation logic. It has no requirement except an Emacs installation and should—in theory—work on all supported platforms and in most Emacs versions, although I only tested Linux and Emacs 26 so far.

Files must be given as command line arguments. It's a script, after all, and doesn't use any graphical interface:

/path/to/edis file1.elisp file2.elisp file3.elisp …

On Windows, a small wrapper is necessary, since it doesn't support shebangs, but that's probably just two lines of CMD or PowerShell.

Mistyped file names should get reported, but other than that, there shouldn't be any restriction on the number of files unless enforced by the OS, shell or Emacs. In theory, you can even use TRAMP to indent remote files, but I haven't tested that exhaustively (try edis /ssh:server:/path/to/file if you're interested).

#!/usr/bin/emacs --script
(defun edis-indent-file (filename)
  "Re-indents a given file"
  (interactive "fFile to indent")
  (cond
   ((file-exists-p filename)
    (find-file filename)
    (indent-region (point-min) (point-max))
    (save-buffer))
   (t
    (message "File not found (need existing file): %s" filename))))

(mapc 'edis-indent-file argv)

The code has been indented with itself. As usual, it will respect file and global variables, so you can specify for example (setq lisp-indent-offset 2) in your init.el or use file local variables, for example:

;; -*- lisp-body-indent: 4 ; lisp-indent-offset: 2 -*-

This is the first Emacs Lisp code I've written so far that's outside my
init.el (where I mostly use use-package, to be honest). Since Emacs is, after all, a text editor, all necessary tools seem to be at hand, so the code isn't that long.

I haven't read the complete Elisp manual yet and got the functions from the C-h k or quick searches in the manual, so I'm not sure whether the code is idiomatic Elisp or completely fails any good practice, so feel free to comment on any part of the code.


texts: Optimizing a search for list of artists in iTunes library

texts:
I'm trying to get a list of all artists on an iPhone / iPod touch music library. The best way I've found has been to group by artists (the convenience constructor on MPMediaQuery) and to iterate over the collections, being one for each artist. That way I've optimized out having to use an NSSet to ensure uniqueness in the artist list. Still, I need to make each collection into a representative item and get it's artist, which according to Time Profiler takes a very good part of this code snippet.

NSString       *artistKey    = [MPMediaItem titlePropertyForGroupingType:MPMediaGroupingArtist];
MPMediaQuery   *artistsQuery = [MPMediaQuery artistsQuery];
NSMutableArray *artists      = [NSMutableArray arrayWithCapacity:artistsQuery.collections.count];

for (MPMediaItemCollection *album in artistsQuery.collections) {
    MPMediaItem *albumItem = [album representativeItem];
    [artists addObject:[albumItem valueForProperty:artistKey]];
}

Wrapping it in some timing blocks, such as below, I've gotten it to run in 0.315 seconds on my iPhone 4 with a library of ~2600 songs and 88 artists, but I feel like that long for only 88 items is way too long.

NSTimeInterval start  = [[NSDate date] timeIntervalSince1970];
// [snip]
NSTimeInterval finish = [[NSDate date] timeIntervalSince1970];
NSLog(@"Execution took %f seconds.", finish - start]);

Does anyone know of a way to make this a bit faster?


texts: Improve a script to generally check status of several git repos

texts:
I've tried to write a bash script that gives me a general overview of the status of git repos I have cloned/created. It's not meant to replace git status by any means, but give an overview of several repos.

Keep in mind I'm not the most knowledgeable about bash scripting AND git, so I imagine there is a better way to do this:

#!/bin/bash
# Help from: http://www.leancrew.com/all-this/2010/12/batch-comparison-of-git-repositories/

index=0
gitrepos=()

#TODO: There has got to be a better way to add all these folders to the array
#add repos folder
for d in ~/repos/*; do
    gitrepos[(index+=1)]="$d"
done

#add other important folders
gitrepos[(index+=1)]=~/.vim
gitrepos[(index+=1)]=~/dot_files
gitrepos[(index+=1)]=~/bin

for d in "${gitrepos[@]}"; do
    if [ -e $d ]; then
        cd $d
    else
        echo " Did not find repo: $d"
        continue
    fi

    reponame="`basename $d`"

    ok=true
    git fetch --quiet origin 2>/dev/null
    if [ ! -z "`git diff HEAD origin/HEAD 2> /dev/null`" ]; then
        echo " $reponame --> Out of sync with origin/HEAD"
        ok=false
    fi
    if [ ! -z "`git ls-files --other --exclude-standard 2> /dev/null`" ]; then
        echo " $reponame --> Untracked files present"
        ok=false
    fi
    if [ ! -z "`git diff --cached --shortstat 2> /dev/null`" ]; then
        echo " $reponame --> Changes to be committed"
        ok=false
    fi
    if [ ! -z "`git diff --shortstat 2> /dev/null`" ]; then
        echo " $reponame --> Changes to be staged/committed"
        ok=false
    fi
    if $ok; then
        echo " OK --> $reponame"
    fi
done


texts: Efficiency and techniques for Matrix class that inherits from Python's list

texts:
I have a class named Matrix, this class inherits from Python's list. The class currently can perform matrix multiplication, adding rows using list.extend, add a row using list.append, and also transpose.

The emphasis is on using only built-in tools.

I would like to know if this code can be made more efficient and readable.
Also, if there are alternative techniques to produce the same result.
a supplementary problem (more appropriate in StackOverflow) : I cannot use copy.deepcopy(A) with A a Matrix object. That's why i use res = Matrix(...) in the multiplication function.

Thanks.

Here is the code, break into three parts.

Global functions :

# Author: Arief Anbiya
# 11 February, 2018

def dot_product(u, v):
    return sum([i*j for i,j in zip(u,v)]

def multiplication(M,N):
    assert type(M)==Matrix or type(N)==Matrix
    res = None;
    if type(M)==type(N)==Matrix and M.ncol()==N.nrow():
        res = Matrix([[0 for i in range(N.ncol())] for j in range(M.nrow())]);
        for i in range(M.nrow()):
            for j in range(N.ncol()):
                res[i][j] = dot_product(M[i], [N[k][j] for k in range(N.nrow())]);
    elif (type(M)==int or type(M)==float):
        res = Matrix([[N[j][i] for i in range(N.ncol())] for j in range(N.nrow())]);
        for i in range(res.nrow()):
            for j in range(res.ncol()):
                res[i][j] *= M;
    elif (type(N)==int or type(N)==float):
        res = Matrix([[M[j][i] for i in range(M.ncol())] for j in range(M.nrow())]);
        for i in range(res.nrow()):
            for j in range(res.ncol()):
                res[i][j] *= N;
    else:
            raise TypeError("M and N should be either a compatible Matrix object, or a constant");
    return res

Class :

# Author: Arief Anbiya
# 11 February 2018
class Matrix(list):

    def __init__(self, the_list):
        super().__init__(the_list);

    def nrow(self):
        m = len(self);
        return m
    def ncol(self):
        n = len(self[0]);
        return n
    def get_dim(self):
        return (self.nrow(),self.ncol())

    def __add__(self, M):
        res = Matrix(self);
        for row in range(self.nrow()):
           dumrow = [self[row][col] + M[row][col] for col in range(self.ncol())];
           res[row]=dumrow;
        return res
    def __mul__(self, M):
        return multiplication(self, M);
    def __rmul__(self,M):
        return multiplication(M, self);

    def add_rows(self, rows):
        super().extend(rows);
    def append(self, row):
        try:
          sum(row); # Check if all numbers
          if len(row)<self.ncol():
             row.extend([0 for i in range(self.ncol()-len(row))]);
          elif len(row)>self.ncol():
             [row.pop() for i in range(len(row)-self.ncol())];
          super().append(row);
        except:
           raise AssertionError('Elements in row must be mumbers');

    def transpose(self):
        return Matrix([[row[i] for row in self] for i in range(self.ncol())]);

    def show(self):
        print("Printing matrix: ");
        for i in self:
           print(i);

Test for outputs :

A=Matrix([[1,2,3], [2,3,3]]);
A.show();
B = A+A;
B.show();
B.append([1,11,12])
B.show();
C = 3*B;
C.show();
D = A*B;
D.show()


texts: Using Dynamic Programming to reduce time complexity

texts:
Input

A long string of numbers, A list of numbers in string

Output

Minimum space needed in long string to match maximum numbers from list

Example

minSpaces('3141592653589793238462643383279',['314','49','15926535897','14','9323','8462643383279','4','793'])
will return 3

I am using below code to solve it, but I am not able to figure out how can I use DP for more efficient time complexity.

Thanks for your help.

dpArr = {} #empty list for DP

def findMinSpaces(k, piStr, favNumArr):
    curr = ''
    ans = 0
    N = len(piStr)
    if( k == N):
        return 0
    for i in range(k, N):
        print(curr)
        curr += piStr[i]
        if(curr in favNumArr and i != N-1):
            if(curr not in dpArr):
                ans += 1 + findMinSpaces(i+1, piStr, favNumArr)
                dpArr[curr] = ans
            else:
                print("from dpArr")
                return dpArr[curr]
    print(dpArr)
    return ans

def minSpaces(piStr, favNumArr):
    return findMinSpaces(0, piStr, favNumArr)
print(minSpaces('3141592653589793238462643383279',['314','49','15926535897','14','9323','8462643383279','4','793']))


texts:
I recently learned quick sort through https://www.geeksforgeeks.org/quick-sort/ but found it hard to follow. So, from what I understand wrote the following program.

#include <stdio.h>
void quick_sort(int[],int,int);
int main(){
    int arr[100];
    int n;
    printf("Enter the elements :");
    scanf("%d",&n);
    for(int i = 0 ; i < n ; i++){
        printf("%d:",i+1);
        scanf("%d",&arr[i]);
    }
    int arr_size = sizeof(arr) / sizeof(arr[0]);
    for(int i = 0; i < n; i++){
        printf("%d ",arr[i]);
    }
    printf("\n");

    quick_sort(arr,0,n - 1);

    for(int i = 0; i < n; i++){
        printf("%d ",arr[i]);
    }
    printf("\n");
}

void quick_sort(int arr[],int pivot_, int right_){
    //Base condtion.
    if(pivot_ == right_ )//pivot = left = right == no more check
        return;
    int i ;
    int pivot, left ,right;
    pivot = pivot_;//first element...
    right = right_;//last element...
    left = pivot + 1; // second element.
    int middle;
    while( left <= right ){
        if(arr[left] >= arr[pivot]){
            if(arr[right] <= arr[pivot]){
                int temp = arr[left];
                arr[left] = arr[right];
                arr[right] = temp;
                right --;
                left ++;
            }else{
                right --; // left > pivot but right !< pivot
            }
        }else{
            left ++;// left is not > pivot.
        }
    }
    i = pivot + 1;
    while(arr[i] < arr[pivot]) i++;
    i--; // last smaller value than pivot encountered.
    middle = i; // swappppppp..
    int temp = arr[pivot];
    arr[pivot] = arr[i];
    arr[i] = temp;
    // now left of i is less than pivot and right of is greater than pivot.

    quick_sort(arr,0,middle);
    quick_sort(arr,middle + 1,right_);
}


texts:
The description of the problem is:

  The prime factors of 13195 are 5, 7, 13 and 29.
  What is the largest prime factor of the number 600851475143 ?

Here is my solution:

import heapq
from math import sqrt

def find_max_prime_factor(n: int) -> int:
    '''
    returns the largest prime factor of the non negative integer n
    :param n: the number we want to find the max prime factor of
    :return: the largest prime factor of n
    '''
    max_heap = []
    # Checking for 2 as prime factor
    if n % 2 == 0:
        heapq.heappush(max_heap, -2)
        # dividing by 2 until n becomes odd
        while n % 2 == 0:
            n = n/2
    # dealing with odd numbers
    for i in range(3,int(sqrt(n)),2):
        if n % i == 0:
            heapq.heappush(max_heap,-1*i)
            while n % i == 0:
                n = n/i

    # dealing with case where n is a prime by itself
    if n > 2:
        return n

    return heapq.heappop(max_heap) * (-1)

Here's some examples and test-cases:

from ProjectEuler.problem3 import find_max_prime_factor

if __name__ == "__main__":
    # 13195 prime factors are 5,7,13,29 -> returns 29
    print(find_max_prime_factor(13195))
    # 600851475143  prime factors are 6857,1471,839,71 -> returns 6857
    print(find_max_prime_factor(600851475143))
    # 7 is prime therefor -> returns 7
    print(find_max_prime_factor(7))
    # 3452656 prime factors are 2,31,6961 -> returns 6961
    print(find_max_prime_factor(3452656))
    # 896776435 prime factors are 5,17,2549,4139 -> returns 4139
    print(find_max_prime_factor(896776435))

Would love for feedback on my code.Thanks!


texts: Find entry in hash depending on hash's data

texts:
I wrote this code and I just can't stomach it.
The idea is that there is a schema, and the goal is to find the entry with autoIncrement set to true. If it's not found, the result should be idProperty.
const idProperty = 'id'
const schema = {
  id: { type: 'number' },
  code: { type: 'number', autoIncrement: true },
  email: { type: 'string', trim: 512 }
}

// Find the auto increment field
let autoIncrementSchemaFieldName = null
for (const name in schema) {
  const entry = schema[name]
  if (entry.autoIncrement) {
    autoIncrementSchemaFieldName = name
    break
  }
}
autoIncrementSchemaFieldName = autoIncrementSchemaFieldName || idProperty

console.log('RESULT:', autoIncrementSchemaFieldName)

Is this really the best way to do it?
(To test whether the idProperty fallback works, just rename autoIncrement into autoIncrement2 and run it again)