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

*

*Components

*Issues

*IssueStates

*IssueTypes

*Priorities

*Projects

*Releases

*Sprints

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

*

*Django Best Practices: Referencing the User Model

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

from django.conf import settings

class Issues(models.Model):

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

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

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

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

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

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

        if not self.slug:

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

            self.slug = slug

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

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

    class Meta:
        managed = True
        db_table = "issues"

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

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

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

    id = models.BigAutoField(primary_key=True)

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

    slug_id = models.IntegerField(default=100)

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

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

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

        if not self.slug:

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

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

            else:
                logging.debug(result)

                next_slug_id = result["slug_id__max"] + 1

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

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

    class Meta:
        managed = True
        db_table = "issue_slugs"

-- issues.py --

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

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

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

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

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

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

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

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

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

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

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

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

    def form_valid(self, form):

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

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

            return super().form_valid(form)

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

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

        return super().form_valid(form)

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

        from ui.models import IssueStates, Priorities, IssueTypes

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

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

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

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

*

*reporter

*reporter_id

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

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

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

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

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


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

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

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

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

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

*

*Don't Do It

*For Experts: Don't do it yet

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

    //Exception when using JobScope for the writer
    @Bean

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

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

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

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

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

        return partitionedStep;

    }

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

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

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

    public abstract FieldSetMapper<T> getFieldSetMapper();

    public abstract String getFilesPath();

    public abstract String getLoaderName();

    public abstract String[] getColumnNames();

}

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

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

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


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

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

A dialog box will report the network state.


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

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

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

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

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

A: Uri uri = uri obtained from ACTION_OPEN_DOCUMENT_TREE

String folderName = "questions.59189631";

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

DocumentFile folder = documentDir.createDirectory(folderName);

return folder.getUri();

Use createFile() for a writable file.


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

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

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

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

*

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

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

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

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


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

Keywords:

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

Town:

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

Property Type:

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

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

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

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

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

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

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

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


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

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

    factorialRec(n, 1)
}

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

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

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

 import scala.annotation.tailrec

 object Factorial extends App {

  val ITERS = 100000

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

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

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

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

}

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

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

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


Q: Create another "Display Site Title and Tagline" checkbox, "Header Text Color" setting and control When you enables Custom_Headers support for your theme with the following code, a "Display Site Title and Tagline" checkbox on the Site Identity section and a "Header Text Color" control on the Color section will appear:
/**
 * Set up the WordPress core custom header feature.
 */
function theme_custom_header_setup() {
    add_theme_support( 'custom-header', array(
        // Default arguments
    ) );
}
add_action( 'after_setup_theme', 'theme_custom_header_setup' );

My question is, how can I create another "Display Site Title and Tagline" checkbox, "Header Text Color" setting and control with the same functionalities as listed?

*

*The color control will deactivate/disappear when the checkbox is unchecked asynchronously, not refresh, and reappear when checked.

*The controls only have 1 setting because when the checkbox is unchecked, the "header_textcolor" setting will turn to have the 'blank' value.

I tried to create 1 setting with 2 controls but doesn't seems to work:
function theme_customize_register( $wp_customize ) {

    $wp_customize->add_setting( 'tagline_textcolor', array(
        'transport'   => 'postMessage',
    ) );
    $wp_customize->add_control( 'tagline_textcolor', array(
        'type'        => 'checkbox',
        'section'     => 'title_tagline',
        'label'       => __( 'Display Tagline Text', 'theme_name' ),
    ) );
    $wp_customize->add_control(
            new WP_Customize_Color_Control(
                    $wp_customize,
                    'tagline_textcolor',
                    array(
                        'label'           => __( 'Tagline Text Color', 'theme_name' ),
                        'section'         => 'title_tagline',
                        )
                    )
            );
}
add_action( 'customize_register', 'theme_customize_register' );

Can you help me with this, or give me a right idea?

A: If the setting and controls are added exactly as they appear in class-wp-customize-manager.php the JQuery script in wp-admin/custom-header.php will toggle the header text color control properly. The theme_supports line must be commented out or removed as it is "used to hide a setting if the theme lacks support for a specific feature".[1]
add_action( 'customize_register', function ( $wp_customize ) {

    $wp_customize->add_setting( 'header_textcolor', array(
        // 'theme_supports' => array( 'custom-header', 'header-text' ),
        'default'        => get_theme_support( 'custom-header', 'default-text-color' ),

        'sanitize_callback'    => array( $wp_customize, '_sanitize_header_textcolor' ),
        'sanitize_js_callback' => 'maybe_hash_hex_color',
    ) );

    $wp_customize->add_control( 'display_header_text', array(
        'settings' => 'header_textcolor',
        'label'    => __( 'Display Site Title and Tagline' ),
        'section'  => 'title_tagline',
        'type'     => 'checkbox',
        'priority' => 40,
    ) );

    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'header_textcolor', array(
        'label'   => __( 'Header Text Color' ),
        'section' => 'colors',
    ) ) );
});


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

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

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

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

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

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

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

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

*

*Compare them independently

*Combine the data into one data frame.

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

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


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

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

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

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

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

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

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

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

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


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

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

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

    }//catch() block

    return returnCN;
}

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

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

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

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

*

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

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

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

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

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

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

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

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

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

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


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

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

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

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


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

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


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

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

Thank you for your time :)

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

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

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


Q: Mod $p$ irreducibility test proof - yet another question This is the statement of the mod $p$ irreducibility theorem:

Let $p$ be a prime and suppose that $f(x) \in \Bbb Z[x]$ with $\deg f(x) \ge 1$. Let $\bar f(x)$ be the polynomial in $\Bbb Z_p[x]$ obtained from $f(x)$ by reducing all the coefficients of $f(x)$ modulo $p$. If $\bar f(x)$ is irreducible over $\Bbb Z_p$ and $\deg f (x) = \deg \bar f(x)$, then $f(x)$ is irreducible over $\Bbb Q$.

The standard proof I've seen in countless places is as follows:

If $f(x)$ is reducible over $\Bbb Q$, then $f(x) = g(x)h(x)$ with $g(x), h(x) \in \Bbb Z[x]$, and both
$g(x)$ and $h(x)$ have degree less than that of $f(x)$. Let $\bar f(x),\bar g(x)$, and $\bar h(x)$ be the polynomials obtained from $f(x), g(x)$, and $h(x)$ by reducing all the coefficients modulo $p$. Since $\deg f(x) = \deg \bar f(x)$, we have $\deg \bar g(x) \le \deg g(x) \lt \deg\bar f(x)$ and $\deg \bar h(x) \le \deg h(x) \lt \deg \bar f(x)$. But $\bar f(x) = \bar g(x)\bar h(x)$, and this contradicts our assumption that $\bar f(x)$ is irreducible over $\Bbb Z_p$.

My question is, why is it necessary that $p$ is prime in this proof? All of the above reasoning seems to remain valid even if $f(x)$ is reduced under modulo $n$, where $n$ is composite?
Thanks.


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

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

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

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

A: Do like this
private boolean isAddShown = false;

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

       isAddShown = false;
    }

}


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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

    return r

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

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

EDIT:

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

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

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

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

np.array_equal(required, flattened)

>>> True


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


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

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

A: What happens in your deploy:

*

*you push changes to your repo

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

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

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

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


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

    public void onComplete();
}

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

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

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

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

longRunningTask.setOnCompleteListener(obj);

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

*

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

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

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

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


Q: How does the candy merchant's lollipop stock work? I noticed that buying a lot of lollipops from the candy merchant causes him to have a stock shortage, temporarily preventing him from selling lollipops.

My questions:

*

*How does the candy merchant's lollipop stock work? How many lollipops can I buy before he gets a stock shortage?

*How long does it take for him to restock? Will he restock every x seconds, regardless of whether I bought all his lollipops or not? Or will he only start restocking once he gets a stock shortage?

A: Each 15 minutes, the stock is updated by this formula:
this.setNbrInStock(this.nbrInStock + 15 + random.getRandomIntUpTo(10));

It adds 15 plus a random value between 0 and 10 of LPs to the stock. The maximum of LPs in stock is 140.
The update will occur, no matter whether you buy something or not, it just will be 140 LPs max.
if(this.nbrInStock > 140) this.nbrInStock = 140;

Funfact: It seems, that originally, it was planned, that the maximum are 100 LPs, as the comment says If > 100, decrease it.


Q: Disable Clamav / Amavis / SA in Postfix on Debian I would like to disable Clamav / Amavis / SpamAssasin in Postfix on Debian Squeeze 6.0.8 as I have I outsourced my AV to other server.
I tried commenting content_filter and receive_override_options in /etc/postfix/main.cf (saved and restarted server) but I still see clamav and amavis process when I run top.

A: Amavis, or amavisd, is the component that relays e.mail messages to the antivirus. Since it does nothing else, and you wish to get rid of this function, you should simply un-install it.


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

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

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

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

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

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

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

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


Q: Delete all files in directories except those whose path are listed in a file Given the following files:
data/A/a.txt
data/B/b.pdf
...
date/P/whatever.log
...
data/Z/z.jpg

I would like to delete all files in the data/A/, data/B/, ..., data/Z/ directories except those files that are situated under one of the directories listed in the file data/dont_clean.txt. For example, if we have data/P listed in data/dont_clean.txt then nothing should be touched under data/P/, etc.
Something like:
find data/ -mindepth 2 -maxdepth 2 -type f -not -path {listed in data/dont_clean} -delete

Of course it is not a valid command.
I have also tried variants of
find data/ -mindepth 2 -maxdepth 2 -type f -exec grep data/dont_clean.txt '{}' \;

but I only created either an invalid command or I had no idea why I got the output I did.
I am using bash on Ubuntu 12.10

A: This is code that I only roughly tested but might layout an approach for you to take. Assuming you have a file, ignore.txt like this:
1/
2/

Sample data
And I had sample directories with files in them like this:
$ mkdir -p dirs/{1..5}
$ touch dirs/{1..5}/afile

Resulting in this:
$ tree dirs/
dirs/
|-- 1
|   `-- afile
|-- 2
|   `-- afile
|-- 3
|   `-- afile
|-- 4
|   `-- afile
`-- 5
    `-- afile

Example run
Now if we run this command against this tree:
$ find dirs/ -type f -print0 | fgrep -zFvf ./ignore.txt
dirs/5/afiledirs/4/afiledirs/3/afile

We can see that we're only getting back the files that are in directories not listed in ignore.txt.
So we can add a rm to the end to remove the non-excluded files.
$ find dirs/ -type f -print0 | fgrep -zFvf ./ignore.txt | xargs -0 rm -f

Checking we can see that it worked:
$ tree dirs/
dirs/
|-- 1
|   `-- afile
|-- 2
|   `-- afile
|-- 3
|-- 4
`-- 5

Problems to be worked out
One big problem with this approach is that the strings in the ignore.txt file might match other portions of the directory structure. So some care needs to be paid to making sure that the strings in this file are unique in the way that you expect.
Some blocking could be put around the strings so that they're anchored to the beginning or the end of the string to protect them.
Details
The above commands are doing the following:

*

*finding all the files under the directory dirs

*filtering out any files that are under a directory present in the igonre.txt file

*passing the filter list via xargs to the rm -f command

A: Sounds like a case for the comm command.
list of files to not delete in "keeper"
ls >current
comm -23 current ../keeper | more

check that it's the correct list
comm -23 current ../keeper | xargs rm

A: xargs and find combination
Demo:
My Files :
[root@mail tmp]# find data/ -type f
data/A/d.txt
data/A/b.txt
data/A/a.txt
data/A/c.txt
data/B/e.txt
data/B/g.txt
data/B/f.txt
data/B/i.txt

Exclude List
[root@mail tmp]# cat exclude
data/A/a.txt
data/B/e.txt

find with xargs
[root@mail tmp]# find data/ -type f $( xargs  -I{} echo -n " -not -path {} " < exclude )
data/A/d.txt
data/A/b.txt
data/A/c.txt
data/B/g.txt
data/B/f.txt
data/B/i.txt

Seems Output is OK, let delete but before that make sure output of following command is OK, then you can remove second last echo command.
[root@mail tmp]# find data/ -type f $( xargs  -I{} echo -n " -not -path {} " < exclude ) | xargs -n1 echo rm -rf
rm -rf data/A/d.txt
rm -rf data/A/b.txt
rm -rf data/A/c.txt
rm -rf data/B/g.txt
rm -rf data/B/f.txt
rm -rf data/B/i.txt

A: The only thing that needs to solved is to create a program that:

*

*reads a line from the stdout,

*then echos this line if it doesn't start with any of the strings
listed in the data/dont_clean.txt file.
I couldn't solve this with a bash script easily so I wrote a little C++ program called my_program that does exactly this. Then I get what I want if I run:
find data/ -mindepth 2 -maxdepth 2 -type f | my_program | xargs rm -f

However, I leave this question still open, I am still interested in a pure shellscript solution.

A: You could do it in two steps: mark (to make them distinctive) and then remove unprotected.
If all these files are from the same user, then you can use the list to change ownership or group to another user. Then use find to remove the rest and then change the user back. If all the files have the same read permissions (so you know how to switch back), you can for instance just remove read permission (to flag them) because it's much easier than changing users (no need for root privileges):
while read file; do chmod u-w -R "$file"; done < "data/dont_clean.txt"
find data/ -mindepth 2 -maxdepth 2 -type f -writable -delete
while read file; do chmod u+w -R "$file"; done < "data/dont_clean.txt"

Edited (added recursion) to protect whole directories. Changed from read to write permissions to avoid problems with chmod recursion (it couldn't read directory it just changed).

A: You could process data/dont_clean.txt into a list of globs to be ignored by bash via GLOBIGNORE e.g. if dont_clean.txt contains directory paths like
data/P
data/some_dir

the result would be GLOBIGNORE=data/P/*:data/some_dir/*;
you would then just remove all regular files that the glob data/*/* expands to:
GLOBIGNORE=$(sed -n 's|$|/*|;H;1h;$x;s|\n|:|gp' data/dont_clean.txt)
for f in data/*/*; do [[ -f "$f" ]] && rm "$f"; done
unset GLOBIGNORE

The above assumes sane directory names in your list. Use printf %s\\n instead of rm if you want to see which files will be removed.


Q: Can't continue deploying after for while via capistrano My environment is following the below.

Environment
Version

Rails
7.0.0

Ruby
3.0.0

capistrano
3.16.0

Production environment
Amazon EC2 Linux

After for while from deploying, I got these logs.
Then deploying stopped, not working anymore.
INFO [41bfeeb4] Running /usr/bin/env sudo /bin/systemctl stop puma_〇〇_production as deploy@〇〇

 DEBUG [41bfeeb4] Command: ( export RBENV_ROOT="/usr/local/src/rbenv" RBENV_VERSION="3.0.0" ; /usr/bin/env sudo /bin/systemctl stop puma_〇〇_production )

 DEBUG [41bfeeb4]

あなたはシステム管理者から通常の講習を受けたはずです。

これは通常、以下の3点に要約されます:

    #1) 他人のプライバシーを尊重すること。

    #2) タイプする前に考えること。

    #3) 大いなる力には大いなる責任が伴うこと。

 DEBUG [41bfeeb4]       [sudo] deploy password:

I think it's related to authorization.
I have no idea for fixing.
How can I do?
deploy.rb
set :application, '〇〇'
set :repo_url, '〇〇'

set :deploy_to, '/var/www/〇〇'

set :puma_threads, [4, 16]
set :puma_workers, 0
set :pty, true
set :use_sudo, false
set :stage, :staging
set :deploy_via, :remote_cache
set :deploy_to, "/var/www/#{fetch(:application)}"
set :puma_bind,
    "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.access.log"
set :puma_error_log, "#{release_path}/log/puma.error.log"
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true
set :puma_restart_command, 'bundle exec puma'
set :rbenv_type, :system
set :rbenv_path, '/usr/local/src/rbenv'
set :rbenv_ruby, '3.0.0'
set :linked_dirs,
    fetch(:linked_dirs, []).push(
      'log',
      'tmp/pids',
      'tmp/cache',
      'tmp/sockets',
      'vendor/bundle',
      'public/system',
      'public/uploads',
    )
set :linked_files,
    fetch(:linked_files, []).push(
      'config/database.yml',
      'config/secrets.yml',
      'config/puma.rb',
      '.env',
    )

namespace :puma do
  Rake::Task[:restart].clear_actions

  desc 'Overwritten puma:restart task'
  task :restart do
    puts 'Overwriting puma:restart to ensure that puma is running. Effectively, we are just starting Puma.'
    puts 'A solution to this should be found.'
    invoke 'puma:stop'
    invoke 'puma:start'
  end

  desc 'Create Directories for Puma Pids and Socket'
  task :make_dirs do
    on roles(:app) do
      execute "mkdir #{shared_path}/tmp/sockets -p"
      execute "mkdir #{shared_path}/tmp/pids -p"
    end
  end
  before :start, :make_dirs
end

namespace :deploy do
  desc 'Make sure local git is in sync with remote.'
  task :check_revision do
    on roles(:app) do
      unless `git rev-parse HEAD` == `git rev-parse origin/master`
        puts 'WARNING: HEAD is not the same as origin/master'
        puts 'Run `git push` to sync changes.'
        exit
      end
    end
  end

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      invoke 'puma:restart'
    end
  end

  before :starting, :check_revision
  after :finishing, :compile_assets
  after :finishing, :cleanup
end

after 'deploy', 'sitemap:refresh'

Capfile
require 'capistrano/setup'

require 'capistrano/deploy'

require 'capistrano/scm/git'
install_plugin Capistrano::SCM::Git
require 'capistrano/rails'
require 'capistrano/rbenv'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
require 'capistrano/bundler'
require 'capistrano/puma'
require 'capistrano/sitemap_generator'
require 'whenever/capistrano'

require 'dotenv'
Dotenv.load

install_plugin Capistrano::Puma
install_plugin Capistrano::Puma::Systemd
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }

A: The deploy user needs to be a sudo user in order to restart the puma systemctl service.
You can fix the issue by creating new file inside /etc/sudoers.d directory and add this line
deploy ALL=(ALL) NOPASSWD:/bin/systemctl


Q: Is the a ScriptEngine or eval()-like function in Swift? In Java, we can build up expressions to be called using ScriptEngine.  This is nice for building up frameworks based on a common naming convention.  In JavaScript, there is of course eval().  Does Swift have some sort of mechanism for evaluating a string which contains a swift expression?  I'm aware that this could be potentially abused; however, it would simplify my present development.

A: No.  Swift is a compiled language, and the runtime doesn't include the compiler.  The iOS SDK doesn't provide a way to evaluate run-time Swift code.
You can execute JavaScript using JavaScriptCore, and JavaScriptCore makes it pretty easy to expose Swift objects and functions to the script.  Maybe that will help you.


Q: Distribution of objects in equal size groups Let there be a large set of objects, each providing a getSize() method that returns a number representing a "size" of some kind. This set is (or can be) sorted according to size.
For simplicity, also assume that:

*

*The group size is larger than the largest object size, so that each group contains at least one object

*There is no limitation to the total number of groups

Given the above, what would be an efficient way of distributing those objects into groups, so that the total size of all objects in a group is (approximately) the same to that of all other groups?
Obviously one can walk the set linearly and put an object into a group if it fits, or create another group and put it there if is does not, but this approach does not achieve equal size distribution.
The problem is independent of programming languages, but an example implementation in Java would also be interesting.


Q: Elasticsearch Python - No viable nodes were discovered on the initial sniff attempt I have a cluster of Elasticsearch nodes running on different AWS EC2 instances. They internally connect via a network within AWS, so their network and discovery addresses are set up within this internal network. I want to use the python elasticsearch library to connect to these nodes from the outside. The EC2 instances have static public IP addresses attached, and the elastic instances allow https connections from anywhere. The connection works fine, i.e. I can connect to the instances via browser and via the Python elasticsearch library. However, I now want to set up sniffing, so I set up my Python code as follows:
        self.es = Elasticsearch([f'https://{elastic_host}:{elastic_port}' for elastic_host in elastic_hosts],
                                sniff_on_start=True, sniff_on_connection_fail=True, sniffer_timeout=60,
                                sniff_timeout=10, ca_certs=ca_location, verify_certs=True,
                                http_auth=(elastic_user, elastic_password))

If I remove the sniffing parameters, I can connect to the instances just fine. However, with sniffing, I immediately get elastic_transport.SniffingError: No viable nodes were discovered on the initial sniff attempt upon startup.
http.publish_host in the elasticsearch.yml configuration is set to the public IP address of my EC2 machines, and the /_nodes/_all/http endpoint returns the public IPs as the publish_address (i.e. x.x.x.x:9200).
We have localized this problem to the elasticsearch-py library after further testing with our other microservices, which could perform sniffing with no problem.

A: After testing with our other microservices, we found out that this problem was related to the elasticsearch-py library rather than our elasticsearch configuration, as our other microservice, which is golang based, could perform sniffing with no problem.
After further investigation we linked the problem to this open issue on the elasticsearch-py library: https://github.com/elastic/elasticsearch-py/issues/2005.
The problem is that the authorization headers are not properly passed to the request made to Elasticsearch to discover the nodes. To my knowledge, there is currently no fix that does not involve altering the library itself. However, the error message is clearly misleading.


Q: Would awarding reputation of +1 for each vote encourage more voting When SO was new, users seemed to vote a lot more than they do today.
Since reputation is intended to reward valued participation in the sites, is there perhaps a need to encourage users to vote more?
Furthermore, if this is implemented, should a reputation recalc be done to add existing user's voting to their reputation?

A:
When SO was new, users seemed to vote a lot more than they do today.

A quick query in the Data Explorer reveals:
year   month   number_of_votes_per_month
----   -----   -------------------------
2010   8       504959
2010   7       486036
2010   6       456449
2010   5       456407
2010   4       430347
2010   3       470651
2010   2       421871
2010   1       436296
2009   12      361790
2009   11      364010
2009   10      346814
2009   9       321173
2009   8       333831
2009   7       335970
2009   6       317373
2009   5       294468
2009   4       266940
2009   3       269150
2009   2       261838
2009   1       257096
2008   12      183431
2008   11      180981
2008   10      270977
2008   9       235731
2008   8       61799
2008   7       6

UPDATE:
However, the average number of votes on answers do appear to be in a steady decline: about 13% lower in August 2010 than the all-time high in January 2009:
year   month   votes_per_answer
----   -----   ----------------
2010   8       2.21991530084699
2010   7       2.24435692744368
2010   6       2.26759340378189
2010   5       2.33139620180944
2010   4       2.26774346471624
2010   3       2.27444972927229
2010   2       2.2805144275629
2010   1       2.355767310253
2009   12      2.34382948701287
2009   11      2.32638737758433
2009   10      2.39763986100524
2009   9       2.44056752609626
2009   8       2.44243704544499
2009   7       2.32734172158977
2009   6       2.43334320922628
2009   5       2.42150861663057
2009   4       2.53494671463945
2009   3       2.55219082832073
2009   2       2.57934649910233
2009   1       2.67963604466184
2008   12      2.36511564836698
2008   11      2.32409950864704
2008   10      2.5333967996192
2008   9       1.87002470171514
2008   8       1.404326054835
2008   7       0.5

A: You're not supposed to be able to award yourself reputation1. You earn reputation... by posting great content.
The theory is similar to how Google ranking is earned by how many people link to your site. You can't give yourself a better Google ranking.

1 The only exception is the minimal points awarded to you for accepting an answer.

A: So all you would need to do to get a lot of reputation is to click the upvote button at random, without contributing anything useful to the site? Sounds like a great way to completely break the system.


Q: Drupal Page View not being added to Main Menu Drupal 7 Views has a "Menu" button that should allow one to add a Page View to the Main Menu. I have cloned a view, modified the "Path" and "Menu" parameter. It seems simple enough, because when I create a View via "Add View" I can add that view to the main menu. But, cloning a view and then modifying the "Menu" params does not cause a menu item to show up in the main menu. I have cleared cache a number of times but still nothing appears in the main menu.
Thanks

A: I know this may be silly, but do you happen to save the view after cloning?
Also, make sure that the path is not already existing within you Drupal site.
A view creates a menu entry, check within you menu items listing that this entry is enabled.


Q: how to normalize the output of neural network in tensorflow 1 I have created a neural network using tensorflow 1.x as below:
def coder(x):
    layer_1 = tf.nn.relu(tf.add(tf.matmul(x, weights['encoder_h1']),biases['encoder_b1']))
    layer_2 = tf.nn.relu(tf.add(tf.matmul(layer_1, weights['encoder_h2']), biases['encoder_b2']))
    layer_3 = tf.nn.tanh(tf.add(tf.matmul(layer_2, weights['encoder_h3']),biases['encoder_b3']))
    return layer_3

What I need is to normalize the output of that neural network. I have tried to do that manually by:
layer_4 = (layer_3  * tf.sqrt(tf.cast(tf.shape(layer_3 )[0], dtype=tf.float32))) / tf.norm(layer_3 )
return layer_4

But, I think this way is not correct, is there a way I can normalize the output of the neural network such as adding a normalization layer or something like that.


Q: Vagrant and symfony2 I'm having kind of a bizzare issue related to installing Symfony2 from within a vagrant environment. The environment is set up correctly and is running a web server that is serving files from a folder that is shared with the vagrant environment that is located in the base directory of vagrant.
Basically, vagrant is initiated in directory foo and then within foo, there is a directory called webroot. Vagrant automagically shares the foo directory. An apache server is set up to run so that webroot is the base http directory. This all works fine and I am able to serve basic HTML, PHP and the MySQL connection is tested to be fine.
I used composer to install vagrant the recommended way, into directory inside /webroot/ called Symfony. All of the files now exist within the correct directory. The configuration is correct  and there are no items that Symfony claims need to be changed in /config.php.
The issue comes when I attempt to load /app_dev.php. It throws an exception claiming that it cannot create a file named cache in the /app directory.
As chmod +a is not supported within the vagrant box I am using, I elected to set permissions by uncommenting umask(0000) in app_dev. Assuming it was a permission problem, I tried using chmod to adjust the permissions both within the vagrant environment and within osx to 777 for everything.
What's strange is that when I chmod a file or directory inside the vagrant environment, it claims to set 777 correctly but then when I ls -l, the permissions have not changed. However, when I chmod a file or directory from OUTSIDE The vagrant environment within the webroot folder, the permissions persist. As symfony does not have r/w permissions within the environment, it cannot create the necessary cache and log files. When i run symfony from the command from osx, everything works fine.
Does anyone have any insight as to how to change the permissions for the /webroot directory so things within the vagrant environment can actually read and write to it as chmod doesn't appear to work?

A: I think it's a question of user rights. Your apache + php is probably launched by root. You have to set rights with root.
Two possibilities :
sudo su
chmod -R 777 app/cache

or
sudo chown -v app/cache
sudo chmod -R 777 app/cache

You will probably have to do the same thing with the log file.
My vagrant file if you need it :
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "precise64"  #Box Name
  config.vm.box_url = "http://files.vagrantup.com/precise64.box" #Box Location
  config.vm.provider :virtualbox do |virtualbox|
      virtualbox.customize ["modifyvm", :id, "--memory", "2048"]
  end
  config.vm.synced_folder ".", "/home/vagrant/synced/", :nfs => true
  #config.vm.network :forwarded_port, guest: 80, host: 8080 # Forward 8080 rquest to vagrant 80 port
  config.vm.network :private_network, ip: "1.2.3.4"
  config.vm.network :public_network
  config.vm.provision :shell, :path => "vagrant.sh"
end

vagrant.sh
#!/usr/bin/env bash

#VM Global Config
apt-get update

#Linux requirement
apt-get install -y vim git

#Apache Install
apt-get install -y apache2

#Apache Configuration
rm -rf /var/www
ln -fs /home/vagrant/synced/web /var/www
chmod -R 755 /home/vagrant/synced

#Php Install
apt-get install -y python-software-properties
add-apt-repository -y ppa:ondrej/php5
apt-get update

apt-get install -y php5 libapache2-mod-php5

#Php Divers
apt-get install -y php5-intl php-apc php5-gd php5-curl

#PhpUnit
apt-get install -y phpunit
pear upgrade pear
pear channel-discover pear.phpunit.de
pear channel-discover components.ez.no
pear channel-discover pear.symfony.com
pear install --alldeps phpunit/PHPUnit

#Php Configuration
sed -i "s/upload_max_filesize = 2M/upload_max_filesize = 10M/" /etc/php5/apache2/php.ini
sed -i "s/short_open_tag = On/short_open_tag = Off/" /etc/php5/apache2/php.ini
sed -i "s/;date.timezone =/date.timezone = Europe\/London/" /etc/php5/apache2/php.ini
sed -i "s/memory_limit = 128M/memory_limit = 1024M/" /etc/php5/apache2/php.ini
sed -i "s/_errors = Off/_errors = On/" /etc/php5/apache2/php.ini

#Reload apache configuration
/etc/init.d/apache2 reload

#Composer
php -r "eval('?>'.file_get_contents('https://getcomposer.org/installer'));"
mv -f composer.phar /usr/local/bin/composer.phar
alias composer='/usr/local/bin/composer.phar'

#Postgres
apt-get install -y postgresql postgresql-client postgresql-client php5-pgsql
su - postgres -c "psql -U postgres -d postgres -c \"alter user postgres with password 'vagrant';\""

A: An updated answer for nfs:
config.vm.synced_folder "www", "/var/www", type:nfs, :nfs => { :mount_options => ["dmode=777","fmode=777"] }

A:
Update as of 15th Jan 2016. Instructions for Vagrant 1.7.4+ and Symfony 3. This works.

On a fresh Ubuntu 14.04 install, ACL was installed but I couldn't use +a or setfacl to fix the permissions issues, and of course, as soon as you change any permissions in terminal in vagrant, they're reset to vagrant:vagrant again.
I added the following to my vagrant file:
# Symfony needs to be able to write to it's cache, logs and sessions directory in var/
config.vm.synced_folder "./var", "/vagrant/var",
 :owner => 'vagrant',
 :group => 'www-data',
 :mount_options => ["dmode=775","fmode=666"]

This tells Vagrant to sync var/logs and var/cache (not to be confused with /var/, these are in the root Symfony directory) and have them owned by vagrant:www-data. This is the same as doing a sudo chown vagrant:www-data var/, except Vagrant now does it for you and enforces that instead of enforcing vagrant:vagrant.
Note there are no 777 'hacks' here.
As soon as I added that, I didn't get any more permissions errors in the apache log and I got a nice Symfony welcome screen. I hope that helps someone!

A: Nothing worked for me other than changing location of cache and logs folder to /tmp
AppKernel.php
public function getCacheDir()
{
    if (in_array($this->getEnvironment(), ['test','dev'])) {
        return '/tmp/sfcache/'.$this->getEnvironment();
    }
    return parent::getCacheDir();
}

public function getLogDir()
{
    if (in_array($this->getEnvironment(), ['test','dev'])) {
        return '/tmp/sflogs/'.$this->getEnvironment();
    }
    return parent::getLogDir();
}