Python - CSV read as one big line

Question: I'm new to Python and I'm learning from a tutorial, and there is this line here that is suppose to read from a .csv file and skip the first line (header):
data = open("banklist.csv", "r").readlines()[1:]
for entry in data:
  #do some stuffs

Problem is, it is not even entering the loop. Removing the [1:] in the open statement and adding print data, I find that the csv seems to be read as one big line, and removing the header removed everything instead:
["First National Bank also operating as The National Bank of El Paso,Edinburg,TX,14318,13-Sep-13,18-Sep-13\rThe Community's Bank,Bridgeport,CT,57041,13-Sep-13,17-Sep-13\rSunrise Bank of Arizona,Phoenix,AZ,34707,23-Aug-13,11-Sep-13\rCommunity South Bank,Parsons,TN,19849,23-Aug-13,5-Sep-13\rBank of Wausau,Wausau,WI,35016,9-Aug-13,4-Sep-13\rFirst Community Bank of Southwest Florida (also operating as Community Bank of Cape Coral),Fort Myers,FL,34943,2-Aug-13,26-Aug-13\rMountain National Bank,Sevierville,TN,34789,7-Jun-13,26-Aug-13\r1st Commerce Bank,North Las Vegas,NV,58358,6-Jun-13,26-Aug-13\rBanks of Wisconsin d/b/a Bank of Kenosha,Kenosha,WI,35386,31-May-13,12-Jul-13\rCentral Arizona Bank,Scottsdale,AZ,34527,14-May-13,26-Aug-13\rSunrise Bank,Valdosta,GA,58185,10-May-13,12-Jul-13\rPisgah Community Bank,Asheville,NC,58701,10-May-13,26-Aug-13\rDouglas County Bank,Douglasville,GA,21649,26-Apr-13,26-Aug-13\rParkway Bank,Lenoir,NC,57158,26-Apr-13,26-Aug-13\rChipola Community Bank,Marianna,FL,58034,19-Apr-13,12-Jul-13\rHeritage Bank of North Florida,Orange Park,FL,26680,19-Apr-13,26-Aug-13\rFirst Federal Bank,Lexington,KY,29594,19-Apr-13,12-Jul-13"]

How do I go about this?
Comment: Python has a csv parser module
Comment: The carriage returns `\r` are not being handled correctly hence the error, use the `csv` module
Comment: I must say I don't quite see why this question was downvoted; the OP showed what he tried, what output was expected, what he got instead. The naive comment of 'use the `csv` module' doesn't actually solve the problem either.
Comment: I could use that module, but any idea why is this happening?
Comment: Maybe a mismatch of newline escape sequences?
Comment: Is this a file that has come from a different operating system, e.g. from MAC OS to Windows?
Comment: You could just split on `\r` like so `lines = data.split('\r')` will produce a list of the lines
Comment: I could use, no, you should use it. You are trying to be smarter than the csv module and trying to reinvent the wheel, but you are not.
Comment: @EdChum: The CSV module doesn't automatically handle `\r` *either*, only opening the file in universal newline mode does. That or use `str.splitlines()` to split an existing string on any of the three newline conventions.
Comment: @MartijnPieters thanks for the info, I think this post like many others is a victim of drive-by down voting, I prefer giving feedback and commenting to educate OP rather than `death by 5 close votes`.
Answer: You are reading a text file that uses a different newline convention from your system; \r where probably your system expects either \n or \r\n.
You can open the file using universal newline support instead with the 'rU' mode:
data = open("banklist.csv", "rU").readlines()[1:]

The smarter way to skip that first line is to use the file as an iterator; loop over it directly. The next() method lets you grab one line at a time, giving you a method to skip the first line:
with open("banklist.csv", "rU") as infile:
    next(infile, None)  # skip the first line
    for line in infile:
        print line

However, if this a CSV file, use the csv module to read the data:
import csv

with open("banklist.csv", "rU") as infile:
    reader = csv.reader(infile)
    next(reader, None)  # skip the first row
    for row in reader:
        print row

The CSV module handles splitting the lines into lists for you:
['First National Bank also operating as The National Bank of El Paso', 'Edinburg', 'TX', '14318', '13-Sep-13', '18-Sep-13']
["The Community's Bank", 'Bridgeport', 'CT', '57041', '13-Sep-13', '17-Sep-13']
['Sunrise Bank of Arizona', 'Phoenix', 'AZ', '34707', '23-Aug-13', '11-Sep-13']
['Community South Bank', 'Parsons', 'TN', '19849', '23-Aug-13', '5-Sep-13']
['Bank of Wausau', 'Wausau', 'WI', '35016', '9-Aug-13', '4-Sep-13']
['First Community Bank of Southwest Florida (also operating as Community Bank of Cape Coral)', 'Fort Myers', 'FL', '34943', '2-Aug-13', '26-Aug-13']
# etc.

Comment: Thanks! That \r looked really suspicious.

Python - CSV read as one big line


Catel MVVM How set two different Item Sources within one ListBox

Question: I am trying to create a view that has a list of expanders and I want when I press to expand one of them a list of names from a different item source to be loaded. So far What I get is when I set the item source to LineRouteCollection to set the header of the expander, even though I bind the expander to show the names from AllStopsCollection the binding "Name" shows the names from the LineRouteCollection source and not the names from AllStopsCollection as I want. Can you take a look at my code and tell me what I am doing wrong?
<ListBox>
    <ItemsControl ItemsSource="{Binding LineRouteCollection}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Expander Header="{Binding Name}" MinHeight="70">
                    <ListBox >
                        <ItemsControl ItemsSource ="{Binding AllStopsCollection}">
                            <TextBlock Text="{Binding Name}"></TextBlock>
                        </ItemsControl>
                    </ListBox>
                </Expander>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</ListBox>

EDIT:
In my View model I load the observable collections (AllStopsCollection & LineRouteCollection) like this:
private ObservableCollection<Route> AllLineRoutes;
private ObservableCollection<StopView> AllRouteStops;

//List of all Routes connected to the selected line
public ObservableCollection<Route> LineRouteCollection // Must be property or DP to be bound!
{
    get { return AllLineRoutes; }
    set
    {
        if (Equals(value, AllLineRoutes)) return;
        AllLineRoutes = value;
    }
}

//List of all stops
public ObservableCollection<StopView> AllStopsCollection // Must be property or DP to be bound!
{
    get { return AllRouteStops; }
    set
    {
        if (Equals(value, AllRouteStops)) return;
        AllRouteStops = value;
    }
}

And I fill the collection with data in the constructor of the class. I am loading the data correctly and I can see it but it just wont appear in the textbox after the expander is expand.
Comment: The issue here is that you need to provide an `ItemTemplate` instead of nesting a textbox instead the `ItemsControl`. You should have the same setup as you have for the first `ItemsControl` in the inner `ItemsControl` with an `ItemsControl.ItemTemplate` containing the textblock, otherwise the binding scope for the textblock is the binding scope of the first `DataTemplate`
Comment: I've updated my answer - your issue is with binding scopes (I was expecting you to have a hierarchy of model data not two flat collections!)
Answer: You have provided an ItemTemplate for the first ItemsControl, but the inner ItemsControl contains a TextBlock which is just nested inside the control
You need to specify an ItemTemplate for the inner control too
<ListBox>
    <ItemsControl ItemsSource="{Binding LineRouteCollection}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Expander Header="{Binding Name}" MinHeight="70">
                    <ListBox>
                        <ItemsControl ItemsSource ="{Binding AllStopsCollection}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Name}"></TextBlock>
                               </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                   </ListBox>
                </Expander>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</ListBox>

Otherwise the binding scope for the most inner TextBlock is going to be the same as the inner ItemsControl resulting in the problem you are seeing
Not sure why you have an ItemsControl inside a ListBox though..? Did you want multiple Expanders inside a ListBox rather than a single list item?
This one works fine for me:
    <ListBox ItemsSource="{Binding LineRoutes}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Expander Header="{Binding Name}" MinHeight="70">
                    <ListBox ItemsSource ="{Binding AllStops}">
                            <ListBox.ItemTemplate>

                                <DataTemplate>
                                    <TextBlock Text="{Binding Name}"></TextBlock>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                    </ListBox>
                </Expander>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

Is that what you wanted?
Edit:
Ok in light of your updates I can see what the error is
You want to keep your eyes peeled for binding errors in your output window.
The AllStopsCollection is not a child of LineRouteCollection so the DataContext for the inner control is pointing to a Route not the ViewModel.
In order to bind to the collection of Stops you need to make sure you point the binding to a control that has the ViewModel as it's DataContext. The easiest way is to use an ElementName binding and bind to the root control (usually called LayoutRoot)
e.g.
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:wpfApplication1="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525" d:DataContext="{d:DesignInstance wpfApplication1:ViewModel}">
    <Grid Name="LayoutRoot">
        <ListBox ItemsSource="{Binding LineRouteCollection}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Expander Header="{Binding Name}" MinHeight="70">
                        <ListBox ItemsSource ="{Binding DataContext.AllStopsCollection, ElementName=LayoutRoot}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Name}"></TextBlock>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ListBox>
                    </Expander>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

Hope that helps
The only thing to watch out for is if you need to manipulate a stop - obviously since you are binding the same collection to multiple sub-lists if you change a Stop item it will change the values in each one of the lists too since the references all point to the same object
Comment: Hmm I think you are right about the ItemTemplate but now tht I have fixed it the problem remains now it does not show anything. But i think you put me on the right track
Comment: Might be something to do with the nesting of controls you have - I'll see if I can produce an example
Comment: Ok I've updated, it seems like you are trying to nest `ItemsControl` inside controls - a `ListBox` *is* an `ItemsControl` so you don't need one nested inside a `ListBox`. I assume you want two listboxes, one nested inside another with different item sources
Comment: Yeah this is what I wanted but. I still cant see the stops when the expander is pressed ;/
Comment: The weird thing is that I can load the names of the stops as names of the headers but I cant load the content in the list box in the expander.
Comment: It sounds like you are just missing the data template - did you update your XAML to remove the `ItemsControl`s from inside your `ListBox`es?
Comment: yep the problem remains
Comment: I don't have the issue - can you post your full code? Including your data item declarations and full XAML again?
Comment: Oh my god thank you very much!!! This problem was bugging me for 4 days!!! GOD and it was that simple thank you again!!!
Answer: That is one approach, try to be more dynamic. Here is a list box where I have predefined 3 different text boxes and provide values at runtime.
<ListBox x:Name="ListBox1" HorizontalAlignment="Left" Height="253" Margin="10,28,0,0" VerticalAlignment="Top" Width="734">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Grid x:Name="grid" Margin="0,2">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="234" />
                                    <ColumnDefinition Width="143" />
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Column="0" HorizontalAlignment="Left" Text="{Binding String1}"/>
                                <TextBlock Grid.Column="1" HorizontalAlignment="Left" Text="{Binding String2}" TextTrimming="WordEllipsis"/>
                                <TextBlock Grid.Column="2" HorizontalAlignment="Left" Text="{Binding String3}"/>
                            </Grid>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

Code Behind :
    public class Class1
    {
        public string String1 { get; set; }
        public string String2 { get; set; }
        public string String3 { get; set; }
    }

    private void Button1_Click(object sender, RoutedEventArgs e)
    {
            ListBox1.Items.Add((new Class1()
            {
                String1 = "Apple",
                String2 = "Car",
                String3 = "Tree"
            }));
    }

OR
If you want a solution that is more dynamic at runtime, you can do something like this:
    private void Button1_Click(object sender, RoutedEventArgs e)
    {
                Grid WholeGrid = new Grid();
                WholeGrid.HorizontalAlignment = HorizontalAlignment.Left;
                ColumnDefinition colDef1 = new ColumnDefinition();
                ColumnDefinition colDef2 = new ColumnDefinition();
                ColumnDefinition colDef3 = new ColumnDefinition();
                WholeGrid.ColumnDefinitions.Add(colDef1);
                WholeGrid.ColumnDefinitions.Add(colDef2);
                WholeGrid.ColumnDefinitions.Add(colDef3);

                // Create Question Lable
                Label QuestionLabel = new Label();
                QuestionLabel.Content = "Apple";
                Grid.SetRow(QuestionLabel, 0);
                Grid.SetColumn(QuestionLabel, 0);

                // Create Date Picker
                DatePicker newDatePicker = new DatePicker();
                newDatePicker.Width = 200;
                newDatePicker.Height = 20;
                Grid.SetRow(newDatePicker, 0);
                Grid.SetColumn(newDatePicker, 1);

                // Add to Lable and Date Picker
                WholeGrid.Children.Add(QuestionLabel);
                WholeGrid.Children.Add(newDatePicker);
                ListBox1.Items.Add(WholeGrid);
    }

Comment: This is not a very MVVM friendly way of doing anything and also doesn't answer the OPs question in any way


Scala and JPA Results Lists

Question: Scala noob i'm afraid:
I have the following declared class variable which will the objects I read from the database:
val options = mutable.LinkedList[DivisionSelectOption]()

I then use JPA to get a List of all rows from a table:
val divisionOptions = em.createNamedQuery("SelectOption.all", classOf[SelectOption]) getResultList
/* Wrap java List in Scala List */
val wrappedOptions = JListWrapper.apply(divisionOptions)
/* Store the wrappedOptions in the class variable */
options += wrappedOptions

However, the last line has an error:
Type Expected: String, actual JListWrapper[SelectOption]

Can anyone help with what I am doing wrong? I'm just trying to populate the options object with the result of the DB call.
Thanks
Answer: What (probably) is happening is that a JlistWrapper[SelectOption] isn't a DivisionSelectOption, so the method += isn't applicable to it. That being the case, it is trying other stuff, and giving a final error on this:
options = options + wrappedOptions

That is a rewriting Scala can do to make things like x += 1 work for var x. The + method is present on all objects, but it takes a String as parameter -- that's so one can write stuff like options + ":" and have that work as in Java. But since wrappedOptions isn't a String, it complains.
Roundabout and confusing, I know, and even Odersky regrets his decision with regards to +. Let that be a lesson: if you thing of adding a method to Any, think really hard before doing it.

Scala and JPA Results Lists


"Transfer" a private GitHub repository to another person / organization

Question: I have a private Github repository that I want to now transfer to a client.  It looks like one way to do that would be to delete the '.git' folder on their local copy and then have them push the local copy to their own GitHub account or other repository.  I don't really like that option because the .git folder would be deleted, although it has 500 MB of data in it.
Another way appears to be to transfer ownership of the repository to them via the GitHub interface, although I'm not sure if they will be using GitHub or another service for the repository.

Presumably, they can also Fork it if I give them access ?  But then not sure what happens to the Fork if I delete my copy.
The goal is to basically transfer ownership and management of the "delivered" package to them, but it would be nice to maintain the .git history, and I have copies of my own in several different places elsewhere.  If I were to just transfer ownership would I also be able to create a clone (on GitHub, not my local system) for myself on Github basically for archival purposes so that I have a record of what I delivered ?
There is an option for that also on GitHub.
Answer: The .git folder can be safely deleted by anyone; deleting .git will not delete the repository itself. It is merely an indication of Git history for a project that has been pulled down from a repository onto a local machine. As such, the client shouldn't have a .git folder at all, unless they have been working on the project themselves. Note that .git folders refer to specific projects, so where theirs is pointing to will depend on how their project has been configured (be it clone, fork, etc.).
If you give them access to create a fork, then they essentially have a clone of your repo. However, be warned that if you delete a private repository all of the forks will be deleted. Retaining copies may come with some legal implications, so double-check with them on what exactly you're allowed to do in this regard. Typically it is the case that once a project is completed and assets are delivered, the client then owns the assets at that point in time.
In my opinion, the best approach to this would be to transfer the project to the client, then request that you're allowed to make a fork of it from your end. This way they can retain the digital rights to it, you can have a copy of it, and they can delete your copy if they so choose.
Comment: That might work.  They don't actually "own" it, more like a permanent license to develop within their organization.  What you suggest might work best though.  I transfer to them, and I can fork that copy because they will still need my help with some things, but they have another developer and Sysop who is going to take over the day-to-day and they will "own" the repo of what I delivered.
Comment: Is there any reason to keep the .git folder really ?  I would like to sort of reset things since this is what we all agree is being delivered.  Also, my github account will still have the history of submits, etc.  That thing is 500 MB.  Did not realize it was so large.
Comment: I've had SSH access to their server.  Been doing the development on my local dev server, push changes to GitHub and then pull down to theirs via SSH.  Everything is in sync that way and I can test everything locally before deploying.


8 - Manually adding a user field will delete all programmatically added user fields

Question: In my module i programmatically add user fields for the website owner instead of instructing them to do so. Here's an example of the code i used which runs in mymodule.install:
<code>function openid_connect_entity_base_field_info(EntityTypeInterface $entity_type) {

if ($entity_type->id() === 'user') {
    $fields['field_birthdate'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Birthdate'))
      ->setDescription(t('Add a description'))
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE)
      ->setDisplayOptions('form', array(
        'type' => 'string_text_field',
        'weight' => 30,
        'default_value' => 0,
      ))
      ->setDisplayConfigurable('form', TRUE);
  }

return fields;
}
</code>
but if you add a field manually all the programmatically added fields will be deleted as shown:
programmatically added fields before adding one manually

Adding a field manually

And boom all the programmatically fields are gone!

If this is a bug in the drupal 8 framework where do i report this?
Comment: I imagine just moving the hook to the .module file instead of .install will fix it. It's not an installation hook
Comment: huzzah! it works, add your suggestion as an answer
Comment: Where are you running that code?
Comment: @Clive in mymodule.install
Comment: I meant function...hook_install probably? How are you actually attaching the fields to the entity type? It'd be useful to see that code
Comment: @Clive I've made an edit
Comment: Alright i'll try that
Answer: You just need to move the hook implementation into the .module file.
It works on install in the .install file because that file is included in the request, and when Drupal looks for hook implementations, it can find it.
Wen you're not installing, e.g. when adding fields through the UI, the .install file is not included so the hook won't be found, and Drupal will consolidate the info it has by removing those fields.


Physical intuition behind Green's theorem?

Question: I get what the theorem is saying. The circulation of the curve is equal to adding up the "microscopic" curls. But why? Why does adding up the "microscopic curls" give you the circulation? Could someone give me an explanation of it using a physical analogy? I.e. an object rotating in moving body of water?
Comment: The whole is the sum of the parts.
Comment: I think the Feynman lectures on physics have a good explanation of Green's theorem.  They are free online.
Answer: The 'microscopic' curls cancel along all their shared boundaries, because they always go in opposite directions. The only thing you're left with is the curl along the 'macroscopic' exterior boundary.
You might also want to ask this question in Physics SE.

Added. See comment below.
Comment: Thanks. What do you mean by "cancel along their shared boundaries"?
Comment: I mean that if you look at two ajoining cells with a clockwise rotation, for example two cells next to each other, the anticlockwise rotation in the left cell is moving down along the common boundary, while the anticlockwise rotation in the right cell is moving up along that common boundary.
Comment: I've added a rough diagram illustrating this: there are cancellations in all the interior boundaries between cells, leaving only the circulation on the exterior boundary. Hope that helps qualitatively.
Comment: The diagram cleared things up immediately - thanks a lot Simon!


Can I change the path to Drush?

Question: I am trying to use omega-wizard but I think I am using the wrong version of Drush. I want to use the drush the is in Acquia Dev Desktop but when i type which drush I get the one that was last installed with MAMP (running OSX).
When trying to run drush omega-wizard I get this message:
The drush command 'omega-wizard' could not be found.  Run `drush cache-clear drush` to clear the commandfile cache if you   [error]
have installed new extensions.
                                                                                [error]
Drush was not able to start (bootstrap) the Drupal database.
Hint: This may occur when Drush is trying to:
 * bootstrap a site that has not been installed or does not have a configured database. In this case you can select another
site with a working database setup by specifying the URI to use with the --uri parameter on the command line. See `drush
topic docs-aliases` for details.
 * connect the database through a socket. The socket file may be wrong or the php-cli may have no access to it in a jailed
shell. See http://drupal.org/node/1428638 for details.

Drush was attempting to connect to:
 Drupal version         :  7.34
 Site URI               :  `http://default`
 Default theme          :  garland
 Administration theme   :  garland
 PHP executable         :  /Applications/Dev Desktop/php5_5/bin/php
 PHP configuration      :  "/Applications/Dev Desktop/php5_5/bin/php.ini"
 PHP OS                 :  Darwin
 Drush version          :  6.2.0
 Drush configuration    :
 Drush alias files      :
 Drupal root            :  /Users/khrysztov/Sites/devdesktop/acquia-drupal-188.8.131.5204
 Site path              :  sites/default
 File directory path    :  sites/default/files

I cannot see any reference to the database there in the parameters and the default theme is not Garland.
Any ideas?
Comment: Just edit your `.bashrc` file and add an alias for drush: `alias drush='php ~/.composer/vendor/drush/drush/drush.php'` (edit to meet your environment).
Answer: Answer by Paul worked (thanks):
alias drush='"/Applications/Dev Desktop/drush/drush"'
Needed inner double quotes to allow for space in Dev Desktop.
Comment: Or just use `alias drush='/Applications/Dev\ Desktop/drush/drush'`.
Comment: Yeah for some reason the escape character wasn't working. I was just pleased to get it working after spending a long time looking for a solution so I was happy with the double quotes.

Can I change the path to Drush?


Validity or invalidity of an argument.

Question: I have to prove the validity or invalidity of the following argument :
Abbots and bishops are churchmen. No churchmen are either dowdy or elegant. Some bishops are elegant and fastidious. Some abbots are not fastidious. Therefore some abbots are dowdy.
It's clear that that no churchmen are either dowdy or elegant,so no bishop can be elegant, and hence the 3rd premis is a contradiction to the 2nd premis.So the premises of the argument are inconsistent and hence the argument form is valid , as it is impossible to find a substitution instance in which the conclusion is false and the premises are true at the same time.So the argument is valid.
Am I thinking in the right direction ? Also is the answer correct ?
Any insight is appreciated. Thank you.
Answer: Yes, that's correct! Some people will say that 'either ... or ..' expresses an exclusive disjunction, rather than an inclusive disjunction, but 'not either A or B' clearly means 'not A and not B'.
And yes, as soon as an argument has an inconsistent set of premises, it is automatically valid, because it is impossible for it to have all true premises and a false conclusion, as you correctly point out.
Good job!

Validity or invalidity of an argument.


Why is the G flat major key signature written with a flat on the lower G and not the higher one?

Question: I was going over the circle of fifths as part of theory, and I was trying to write out the G-flat key signature.  I put the G-flat in where the red flat is in the image below, but I realized that the signature actually uses the G closer to middle C. Is there a reason why this is the case?
Comment: Well, by using the G-flat highlighted in green, it keeps the pattern of the flat being one note lower than the note two before it. It also just looks nicer that way, in my opinion.
Comment: @Aaron  that's not what "begs the question" means.
Comment: @CarlWitthoft Sure it is. It means that it brings up additional unasked questions. For example, your comment, since it doesn't address the OP and is directed specifically at me, begs the question of why you felt it imperative to post it.
Comment: @Aaron, and CarlWittholft, in case anyone really cares, I'd confirm Carl's remark that "begs the question" at least _formerly_ meant "assume the conclusion", which is nearly opposite of its use today. :) But, well, even if usage doesn't determine "correctness", it manifestly ... reflects usage/understanding? :)
Comment: @CarlWitthoft Well, I'm with you there. My pet peeves are "comprise", omitting the Oxford comma, and split infinitives. (Regrettably, the last time I looked up "comprise", it had become acceptable to use it as a synonym for "compose". I protest!) Now get off my lawn!
Comment: Interesting. It begs the question of why any of the elements in a key signature are placed as they are. Eb, for example, could just as well be on the treble staff's bottom line. And one could suppose in the Gb case that the goal is to keep everything within the staff, but that doesn't hold in the G# case.
Comment: @Aaron  I posted it because I'm a GrumpyOldMan(TM) who is sick and tired of people who never learned English  putting on airs by misusing this phrase, the words "unique,"  "comprise,"   or "impact," and others.
Answer: Here's the layout for sharps and flats in the main clefs. Sometimes it can be justified by 'maintain the pattern'  or 'keep it all in the stave'.  But some decisions are just convention, Why A rather than B for treble clef sharps?  Why aren't bass clef flats a mirror of bass clef sharps, rather than being allowed to fall off the bottom of the stave?  I don't think 'why?' is going to get us very far here!

I don't think the convention is much disputed.   Here's what Gould has to say.
Comment: @Aaron "That's how it is" **is** the answer.
Comment: @PiedPiper Key signatures didn't evolve arbitrarily. "That's just how it is" is effectively the same as "I don't know."
Comment: This question reminds of the usual [question](https://math.stackexchange.com/questions/25333/why-does-0-1) that most math students ask at some point: why is the factorial of 0 equal to 1? And the answer is: **it's convention**! Asking why the convention is the way it is can bring insight, as in the question I linked. In this case (and unlike the math question I linked), I think that using a different convention will not change anything because the information will be the same, and the why of it doesn't seem as important. It's likely just a more intuitive or easy-to-the-eye notation
Comment: @Laurence That is a motivation for the definition of n! and 0!, not the actual formal definition. Definitions in math are prepared around such intuitive concepts (counting, etc) but are worded in a very formal unambiguous way. We define n! as the product of the first n integers (for n>0). 0! is then set to 1 as a convention. In my opinion, the convention 0!=1 is used because many formulas that use n! (such as the Taylor series and the Binomial coefficients) still hold for n=0 with it. If it were more convenient to have 0!=0 in the formulas, motivations for that choice could be found, I think
Comment: This doesn't really answer the question. It just confirms that "that's how it is."
Comment: We can do a bit better than 'convention' in justifying "0!=1".   Here's one way:

n! counts the number of ways to arrange n distinct objects in a line, and there is only one way to arrange nothing.
Comment: That's weird - I *definitely* remember seeing the key signature in B in sheet music in YouTube videos before.
Comment: @Gandalf Agreed.  But it's one up on just saying 'convention'.
Comment: @Laurence Yes, you are right. I agree that your and others' answers are strictly better and more insightful than simply saying it's convention! Reading my comments again, I realized I wasn't very clear with this, but the point I was trying to make with this 0! analogy was that IMO the answer to OP's question is _"it is the convention"_, but saying that the G flat goes to that position to keep the going-down pattern or keeping the flats/sharps in the main lines/spaces of the sheet (or others) are answers to a different question, which is _"what are the motivations for this convention?_.
Comment: I feel so lucky and thankful that the convention doesn't happen to be like randomly all over the place. Why would it be? Why not? (If the "why" question doesn't get us far, maybe the "why not" question gets further...)
Comment: @piiperi   As I stated in my answer, tidiness and consistency explains most of it.  But not quite all.
Answer: Laurence's answer has it all laid out. Sometimes the answer is simply: "Because that's the way they did it a few hundred years ago and that's the way we still do it". However, there is a certain logic to it.
If you look beyond that you will see a certain patterns or hierarchies emerge. One pattern is that for flats it is always up a 4th then down a 5th. That creates the stair pattern we are all used to. For sharps it is down a 4th then up a 5th except for when it starts to get a bit high, then it's down a 4th twice in a row (G#, D#, A#). Another thing is it never goes up or down two 5ths in a row. All this results in a clean and easily recognizable look for all the key signatures.
Comment: Bit like we don't count the dots on a die - it's the *pattern* that's recognised.
Comment: @Tim That's a good analogy.
Answer: The short answer is:  it's just the traditional way of laying out the key signature.  Key signatures aren't as much read as they are recognised — and messing with the normal arrangement is only going to mean that it would take slightly longer for the performer to recognise what the key signature is, and for no gain.

A more in-depth answer is that it looks much neater this way.  Consider the conventional layout for the flat and sharp key signatures in the four clefs still in regular use today:

The pattern for flats in the key signature is that they go:  up a fourth, then down a fifth, and repeat.  This simple pattern keeps the flats towards the middle of the staff, and can be applied to all the clefs.  The only compromise is that F♭ appears in the first space below the staff for bass clef, (but seven flats is very rare).
If G-flat was up rather than down, it wouldn't work for tenor clef, as it'd require a leger line.

To keep the sharps of a key signature toward the middle of the staff, there is a different pattern (with two downs in a row), the sharps go:  down a fourth, then up a fifth, down a fourth, and repeat.  This keeps all the sharps near to the middle of the staff, with the compromise that G♯ occurs in the space above the staff in treble clef.
However, this sharps pattern doesn't work for tenor clef (the rarest clef still in common use) — it would put the G♯ on a leger line.  So, the pattern for tenor clef's key signature with sharps is different, it goes:  up a fifth, then down a fourth, (and repeat).
Why isn't the tenor clef's (simpler) sharps pattern used for the other clefs?

For bass clef this pattern would put the first sharp (F♯) below the staff, which is not aesthetically pleasing.
Tradition.
Comment: I don't agree that key signatures are 'recognised', as much as *how many  #/b are there?*. (except by constant use). Doubt if anyone actually looks at any key signature and works out which notes are # or b!  Simply the number of them should be enough. I used to read music where the composer wrote, say in key A, 3# any old where. We all knew where they were supposed to be - where else *could* they be?! And often use fingers to denote keys - no lines or spaces to bother with then!
Comment: @Tim For example, when I see four flats (in the conventional places) I instantly think "that's A-flat major / F minor".  I don't think "B-flat, E-flat, A-flat, D-flat", and don't have to count them "one, two, three, four" either.  Any key signature that I don't recognise and I think ["Why? Bartók why?"](https://i.stack.imgur.com/2Jjbq.png)
Comment: With you all the way with Bartok. Tried and failed! But just about any other music, yes, 4b would indicate to most musos Ab/Fm. But instinctively we see the key sig. and translate it. When it gets to more than 5#, I need to 1) think about it, or 2) fiddle with the key change on the electric keyboard!, or 3)abort...
Answer: It's just the convention to list the flats by ascending fourths then descending fifths.
To underscore the point about conventions, and to show those conventions can change, look at this old edition of Couperin...

...the flats are not all staggered and they are repeated at the octave.
You could be unconventional and put the G flat in the higher octave, and people would understand it's a key signature of 6 flats.
My point is it isn't written in stone, nor purely logical, it's a convention.
https://imslp.org/wiki/Pi%C3%A8ces_de_clavecin%2C_Livre_1_(Couperin%2C_Fran%C3%A7ois)
Comment: Paper becomes cheaper, as the modern arrangement takes more space?  Just speculating :-).  I wonder whether notation software such as Sibelius and Finale accommodate this ...
Comment: Even the time signature is upside-down.


Google Sheets - Query date < doesn't work

Question: I'm using google sheets, and I found that this doesn't work:
<code>=QUERY(CSV!1:1000,"select J, AA, AD where J<date'2004-01-01'")
</code>
But this does:
<code>=QUERY(CSV!1:1000,"select J, AA, AD where J>date'1900-01-01' and not J>date'2004-01-01'")
</code>
Can someone please explain why I have to make my query more complicated to filter by date?
Answer: try:
<code>=QUERY(CSV!1:1000, "select J,AA,AD where J < date'2004-01-01' and J is not null", )
</code>
Comment: That worked...but why? I had 4 non-null results but none get returned unless the null check exists?
Comment: @user5061526 most likely they were returned but at the bottom of the sheet coz null rows came first/on top


Windows Batch - How to search muliple variables then create new variables with their name

Question: I have a batch file that pulls in all the command line arguments and stores them as variables. I then search for a name in each, then make a new variable of that name but with new content (rem). I have the first search done here but I don't know how to search all the others without repeating the same commands. I tried it by doing that and it didn't work. Anyway if there is an easier way than this please tell.
@echo off
setlocal enableDelayedExpansion
set var1=noisereduction-01
set var2=sharpener-03
set var3=resize-02
set var4=output-01
set var5=grain-02
set var6=crf-21
set var7=preset-veryfast
set var8=tune-film
set var9=ref-2
set var10=outputdir-g:\output
set var11=mode-3

for /l %%N in (1 1 11) do call :test var%%N
exit /b

:test  variableName
echo !%1!|>nul findstr /rx .*noisereduction.* && set !%1!=rem
echo %1 = !%1!
exit /b

Whoops, there is an error above. I want to create a new var with the name of "noisereduction-01" and a value of "rem", not just give var1 a value of rem. I thought the set !%1!=rem would do it but it is not.
Answer: Let's see this problem. When
set var1=noisereduction-01

... and
for /l %%N in (1 1 11) do call :test var%%N

... then the first call to test is with "var1" parameter. This mean that into test:
set !%1!=rem  IS  set !var1!=rem  IS  set noisereduction-01=rem

... that correctly do what you want.
HOWEVER:
echo %1 = !%1!  IS  echo var1 = !var1!  IS  echo var1 = noisereduction-01

If you want to show the value of noisereduction-01 variable, then you must perform an additional expansion step:
for %%v in (!%1!) do echo param: %1, variable: !%1!, value: !%%v!

Comment: Ok, you are correct. So how do I do this for all the variables in my list, a "for" command??
Comment: @tyee - if you need to control something in Batch, mostly you need a `for` loop.
Comment: @tyee: You already have the `for /l %%N in (1 1 11) do call :test var%%N` loop to review all the _variables_ in your list, just add the additional expansion inside `:test` subroutine. I am missing something?


Replace For-Each loop using Streams

Question: I want to replace the following for-each loop using Java 8 Streams:
for (Rule rule : this.rules) {

    if (rule.getCondition().evaluate(rule, record)) {
        Records.emit(collector, outputStreamMapping.get(rule.getOutputStream()), tuple, recordId, record);
        collector.ack(tuple);

        ruleApplied = true;
        break;
    }
}

if (!ruleApplied) {
    LOGGER.warn("No rule was applied to record {}", record);
    LOGGER.debug("Rules: {}", this.rules);
    ErrorReporter.emitErrorNode(this.collector, recordId, componentName,
            "No matching rule for record " + record, record);
    collector.fail(tuple);
}

I want to iterate over a set of Rules, and evaluate each Rule's Condition.  If the condition applies, I act on that record and stop processing.  If no Rules are applied, then I want to log that and perform different processing of the record.
I'm not exactly sure how to do this, though.  Any help and explanation would be greatly appreciated.
EDIT:
I've tried this:
    this.rules.stream().filter(rule -> rule.getCondition().evaluate(rule, record)).forEach((rule) -> {
        Records.emit(collector, outputStreamMapping.get(rule.getOutputStream()), tuple, recordId, record);
        collector.ack(tuple);

        ruleApplied = true;
        break;
    });

It doesn't like the break statement, of course, and also complains about ruleApplied not being final since it's declared outside the scope of the lambda.
Based on the answers I'm seeing, it seems as though the loop is the cleanest way to do this.  I wasn't sure if there were other stream constructs that would allow me to encapsulate the logic (i.e., the break and the tracking boolean) in a different manner than how I did in the basic loop.
EDIT2:
Here's my solution based on this thread's advice:
Optional<Rule> possibleRule = rules.stream().filter(rule -> rule.getCondition().evaluate(rule, record))
        .findFirst();

if (possibleRule.isPresent()) {
    Records.emit(collector, outputStreamMapping.get(possibleRule.get().getOutputStream()), tuple, recordId,
            record);
    collector.ack(tuple);
} else {
    LOGGER.warn("No rule was applied to record {}", record);
    LOGGER.debug("Rules: {}", this.rules);
    ErrorReporter.emitErrorNode(this.collector, recordId, componentName,
            "No matching rule for record " + record, record);
    collector.fail(tuple);
}

Comment: The `ruleApplied = true` line in particular is going to be difficult.  The result is not going to be as nice as the code you already have.
Comment: @LouisWasserman: Filter, then project with a side-effect, and count the result? Horrible, but it would work...
Comment: @LouisWasserman agreed. But it's still possible
Answer: You can do
Optional<Rule> rule = rules.stream().
                       .filter(rule -> rule.getCondition().evaluate(rule, record))
                       .findFirst();
if (rule.isPresent()) {
    Records.emit(collector, outputStreamMapping.get(rule.get().getOutputStream()),
                 tuple, recordId, record);
    collector.ack(tuple);

    ruleApplied = true;
}

Comment: You don't need separate variable ruleApplied. The logic you want to execute if no rule is applied can simply be put inside else block.
Comment: @MrinalK.Samanta Correct, You would need to see how ruleApplied is being used, but the code which checks it might be merged into this `if` or `else` block. +1
Answer: You should be able to do this with Stream's anyMatch method and a Predicate that performs the necessary side effect of Records.emit, etc.
boolean ruleApplied = this.rules.stream().anyMatch(
    rule ->
    {
        boolean match = rule.getCondition().evaluate(rule, record);
        if (match)
        {
            Records.emit(collector, outputStreamMapping.get(rule.getOutputStream()), tuple, recordId, record);
            collector.ack(tuple);
        }
       return match;
   }
);

The anyMatch method is a short-circuiting operation that will stop processing when the Predicate returns true.  The short-circuit will fulfill the break behavior you have.

Returns whether any elements of this stream match the provided predicate. May not evaluate the predicate on all elements if not necessary for determining the result. If the stream is empty then false is returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
Comment: @PeterLawrey's solution is much more straightforward and doesn't abuse side effects.  I strongly recommend that solution instead; side effects in `Predicate`s are deeply frowned upon by the documentation.
Comment: this seems like exactly what i want.  i'll try it out and update with my findings.
Comment: reading into docs more, you're right.  for my specific use-case, the coupling of the expression evaluation and the action based on that is not problematic, but the *practice* is.  thanks for the caution

Replace For-Each loop using Streams


django: how to get Product and ImageProduct

Question: I have two models/tables:

Product
ImageProduct

ImageProduct has ForeignKey to Product
class ImageProduct(models.Model):
    url = models.ImageField(max_length=350,
                            null=True,
                            blank=True,
                           )

    product = models.ForeignKey('Product',
                                null=True,
                                blank=True,
                                on_delete=models.CASCADE,
                               )

I want to get last product with image product in indexpage, so I use:
last_product = Product.objects.all()

last_product returns all products
How to get ImageProduct for each product and use in template:
    {% if last_product %}{% for product in last_product %}

{{ product.name }}
[ 3 روز پیش ]

    {% endfor %}{% endif %}

Answer: As it is currently coded, your ImageProduct has an many-to-one relationship to Product. This essentially means that a Product can have many ImageProducts.
Please also note that the variable name last_product is misleading, it should be eg. products to avoid confusion, as it is a queryset and not a single model.
So you could access the last product with something like last_product = products.last().
You can access the related set of ImageProducts from the parent with this:
last_product.imageproduct_set

If a model has a ForeignKey, instances of the foreign-key model will have access to a Manager that returns all instances of the first model. By default, this Manager is named FOO_set, where FOO is the source model name, lowercased. This Manager returns QuerySets, which can be filtered and manipulated as described in the "Retrieving objects" section above.

Reference: Following relationships "backward".
See also the documentation for related_name if you wish to change the imageproduct_set.
Comment: Exception Value:
'QuerySet' object has no attribute 'image_product_set'
Comment: Fixed typo. It should be `imageproduct_set`. Please read the linked documentation though.


Como ler corretamente uma documentação de um comando SQL?

Question: Estava estudando o conceito de transações e como sempre a referência foi a documentação da própria Microsoft (no caso para os produtos da mesma).
No link abaixo há a documentação do comando BEGIN TRANSACTION.
https://docs.microsoft.com/pt-br/sql/t-sql/language-elements/begin-transaction-transact-sql?view=sql-server-2017
A primeira linha do bloco sintaxe é:
BEGIN { TRAN | TRANSACTION }
O uso de chaves "{}" representa o que?
O uso de pipe "|" representa que eu posso optar em utilizar o comando TRAN ou TRANSACTION?
O uso de colchetes "[]" representa comando opcionais?
Comment: O uso de chaves é um agrupador de um campo obrigatório e serve para indicar as opções disponíveis. Nesse caso tem TRAN e TRANSACTION divididas por pipe, o que indica que pode usar a abreviação ou o nome completo. o uso de colchetes indica algo opcional.
Comment: A origem da notação está na BNF ou Backus-Naur Form: https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_form
Answer: A notação para documentação significa o seguinte:
{ } ou < >:  o que está dentro de chaves é obrigatório, deve ser usado
{ x | y } ou < x | y >: deve ser escolhida uma opção, x ou y, é obrigatório
[  ]: o que está dentro dos colchetes é opcional, pode ser usando ou não
Aqui mais detalhes sobre a notação usada na sintaxe: Sintax notation
Analisando seu exemplo:
1 BEGIN { TRAN | TRANSACTION }
2     [ { transaction_name | @tran_name_variable }
3       [ WITH MARK [ 'description' ] ]
4     ]
5 [ ; ]

O comando começa com BEGIN e deve obrigatoriamente ser seguido por uma das opções ou TRAN (versão resumida) ou TRANSACTION
O [ indica que é opcional declarar ou não variáveis
É opcional declarar ou não WITH MARK e ainda é opcional a descrição
Esse colchete encerra o que foi aberto na linha 2
É opcional encerrar com o ;

Outro exemplo é o Comando SELECT:
SELECT [ ALL | DISTINCT ]
    [TOP ( expression ) [PERCENT] [ WITH TIES ] ]
    < select_list >
    [ INTO new_table ]
    [ FROM { <table_source> } [ ,...n ] ]
    [ WHERE <search_condition> ]
    [ <GROUP BY> ]
    [ HAVING < search_condition > ]

Veja que no comando SELECT,  usar ALL ou DISTINCT é opcional, assim como INTO e WHERE por exemplo, mas o < select_list > é obrigatório, ou seja, a lista de campos, variáveis ou valores do comando SELECT
Um simples SELECT 1 então funciona, baseado na descrição da sintaxe acima :)
Comment: Agradeço! Ficava confuso e acaba optando por fontes não oficiais! Tks!
Answer: As chaves são uma forma de agrupamento, significa que tudo que está ali dentro é uma coisa só, é como se fosse parênteses em uma expressão de programação normal que elimina a ambiguidade. Então obviamente dentro tem mais de um elemento, afinal não teria porque agrupar só uma coisa. Neste caso tem algo que deve escolher por isso tem o | (é um OU), portanto é uma coisa ou outra, e neste caso específico ele permite a forma completa da palavra ou a forma abreviada.
Os colchetes de fato indica que algo é opcional e se não usar não afeta a sintaxe, pode afetar a semântica em boa parte dos casos, mas em SQL tem muito caso que é só simplificação de sintaxe, o mesmo ali em cima poderia ser documentado assim:
BEGIN TRAN[SACTION]

Coloquei no GitHub para referência futura.
Mas não é comum fazer assim, fica um pouco menos claro.
Comment: Se a pessoa que negativou puder indicar o que tem de errado na resposta eu posso consertar, mas aparentemente não tem nada porque o que está escrito aqui é o mesmo da outra resposta que não recebeu negativo.


Como fazer um botao gravar dados de um formulario com eventos de click

Question: Eu tenho um formulário e desejo salva-lo quando for pressionado o botão salvar, a solução que eu fiz não esta funcionando.
<code>public class SalvarFormulario extends AppCompatActivity {

EditText Nome;
EditText edtTelefone;
EditText edtObservacao;
EditText edtEmail;
Button btSalvar;

FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_formulario);

    Nome = (EditText) findViewById(R.id.Nome);
    edtEmail = (EditText) findViewById(R.id.edtEmail);
    edtObservacao = (EditText) findViewById(R.id.edtObservacao);
    edtTelefone = (EditText) findViewById(R.id.edtTelefone);
    btSalvar = (Button) findViewById(R.id.btSalvar);

    InicializaFirebase();
    SalvarDados(); // <--- salva os dados
}

private void InicializaFirebase() {
    FirebaseApp.initializeApp(SalvarFormulario.this);
    firebaseDatabase = FirebaseDatabase.getInstance();
    databaseReference = firebaseDatabase.getReference();
}

private void limparCampos() {
    Nome.setText("");
    edtTelefone.setText("");
    edtObservacao.setText("");
    edtEmail.setText("");

}
</code>
Comment: Você deve criar o método `SalvarDados()` se não foi criado ainda, não da pra saber pois o código não está completo.
Comment: ja criei:

public void SalvarDados() {
        // if (btSalvar.isSelected()) {

        Cadastro c = new Cadastro();
        c.setUid(UUID.randomUUID().toString());
        c.setNome(Nome.getText().toString());
        c.setTelefone(edtTelefone.getText().toString());
        c.setEmail(edtEmail.getText().toString());
        c.setObservacao(edtObservacao.getText().toString());
        databaseReference.child("Cadastro").child(c.getUid()).setValue(c);

        limparCampos();
        }
Comment: Então veja se a resposta dada pelo @NatanFelipe resolve sua dúvida.
Answer: Você tem que pegar o seu button e utilizar o método setOnClickListener e lá você chama o método responsável por salvar os dados, dessa forma:
<code>         botao.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             salvarDados();
         }
     });
</code>


Combinations and permutations problem (Low)

Question: I have an interesting question which goes as follows:

The number of ways in which a host lady can invite for a party of 8 out of 12 people of whom two do not want to attend the party together is.
$(a) 2 × C(11,7) + C(10,8)$
$(b) C(10,8) + C(11,7)$
$(c) C(12,8) - C(10,6)$

Now I know one of the methods to solve it.
Say the two friends are A and B. The number of ways A can attend the party when B isn't = C(10,7) = number of ways you can choose 7 people out of 10, by fixing A to attend the party and B to not attend the party.
The same follows for B.
So the number of ways either A or B can attend the party = 2 × C(10,7)
Now we add in this, the number of ways when people can attend the party, when both A and B don't attend i.e. C(10,8) = choosing all 8 people out of 10 (leaving A and B)
This tells me that the first option is slightly varying from the answer.
The second option is kinda same as first, only it does not consider B attending the party when A isn't, or vice-versa.
Coming to option three.
It seems to say that you find all the combinations in which people can attend the party and subtract something from it to get the answer.
What my question is, I'm not getting what that "something" is and where does it come from? (Somethin' = C(10,6))
Answer: The third option applies when both $A$ and $B$ attend the party. We have $10$ people left and we need to choose $6$ of them to complete a party of $8$.


Is it possible to get a breakdown of the bandwidth usage by file in AWS CloudFront?

Question: My sites traffic is finally starting to peak, and I am a heavy user of AWS CloudFront.  I'd like to optimize our bandwidth, but I don't see anyway of getting a detailed breakdown of which files are contributing most to the bandwidth costs.
Answer: You can enable logging in Cloudfront, which will generate a log file containing a list of requests (and placed into an S3 bucket). You can then parse those log files as you would with server logs to determine the files, IPs, or even edge servers using the most bandwidth.
There are also a few programs that will provide information about the logs - I believe Cloudberry and Bucket Explorer are some of them.
The files are gzipped and added to your bucket approximately hourly.
The format of the file resembles the following:
<code>date time x-edge-location sc-bytes c-ip cs-method cs(Host) cs-uri-stem sc-status cs(Referer) cs(User-Agent) cs-uri-query
2011-06-27  08:31:10    JFK1    587 xxx.xxx.xxx.xxx GET xxxxxxxxxxxxxx.cloudfront.net   /path/to/your/file  304 http://www.mydomain.com/page/requesting/file    User-agent-string
</code>


VB6 Extract Images (any kind) from a ImageList

Question: Extract Images (any kind) from a ImageList and save it as a file with extension
Comment: Exact duplicate of [this](http://stackoverflow.com/questions/4051859/how-to-restore-the-images-uploaded-for-imagelist-control-in-vb-6-0)
Answer: Have you already tried using the ExtractIcon method? That will return an IPictureDisp object representing an icon of the ListImage item.
Then, once you have extracted the picture object, you can simply call the SavePicture function to save it to a file on disk.
So you could use something like the following code:
Dim myPic As Picture
Set myPic = ImageList1.ListImages(1).ExtractIcon
SavePicture myPic, "C:\MyIcon.ico"  '(never hardcode a path in your actual code!)

Answer: I think this is what you want: http://btmtz.mvps.org/gfxfromfrx/
I found pretty handy.


Fit error-in-variables polynomial regression using mle2 (R)

Question: I need to fit a polynomial regression that accounts for measurement errors. I found out how to do it with a mcmc model (using RJags) and I would like to do it with a Maximum Likelihood Estimator (using mle2 function in R), since the model will be later more complex and mle2 will be faster than mcmc.
My model in RJags looks like this (I put some data to make the code reproducible):
modelFile = "model.txt"
modelString = "
model {
# Likelihood:
for (i in 1:N) {
y[i] ~ dnorm(y.hat[i], tauy[i])
y.hat[i] <- b[1] + b[2]*x.hat[i] + b[3]*z.hat[i]

x.hat[i] ~ dnorm(x[i], taux[i])
z.hat[i] ~ dnorm(z[i], tauz[i])

taux[i] <- 1/pow(sdx[i],2)
tauy[i] <- 1/pow(sdy[i],2)
tauz[i] <- 1/pow(sdz[i],2)
}

for(j in 1:3) {b[j]~dunif(-2,2)}

}
"
writeLines(modelString,con=modelFile)

#Data

 ind <- data.frame(A = c(2.428, 2.601, 2.749, 2.553, 2.753, 2.421, 2.579, 2.415, 2.407, 2.509),
                  B = c(0.95, 0.99, 1.05, 1.00, 1.04, 0.96, 1.01, 0.95, 0.95, 1.01),
                  C = c(-0.04, -0.09, 0.01, 0.04, -0.15, 0.11, -0.17, -0.12, -0.13, 0.17),
                  eA=runif(10, 0, 0.5), eB=runif(10,0,0.5), eC=runif(10,0,0.2))

ml.data <- list(x=ind$A,
                    y=ind$B,
                z=ind$C,
                    sdy=ind$eA,
                sdx=ind$eB,
                    sdz=ind$eC,
                N=nrow(ind))

ml.par <- c("b")

ml.mod <- jags.model(modelFile,data=ml.data, n.chains=100, n.adapt=1000)

update(ml.mod, n.iter = 1000)

mcmc.out <- coda.samples(ml.mod, var=ml.par, n.iter=10000)

#summary of the posterior distributions of the parameters
summary(mcmc.out)

How can I translate this into an mle2, or how the function would be?
Answer: The model you are describing is a little bit strange... You have $i=1,...,N$ random variables $X_i$, $Y_i$, and $Z_i$'s, with each of them having their own mean $x_i$, $y_i$, $z_i$, and their own standard deviations $\tau_X$, $\tau_Y$, and $\tau_Z$  (in your case precision, but this is of less importance in here). There are three unknown parameters $\beta_0$, $\beta_1$, and $\beta_2$.
If you are familiar with R, than translating "likelihood" from your JAGS code to R would lead to
dnorm(y, mean = b[1] + b[2]*rnorm(n, x, sdx) + b[3]*rnorm(n, z, sdz), sd = sdy)

...are you sure you want $\hat x$ to be just some random number drawn from distribution with mean $x$ and standard deviation $\tau_X$..? (In most cases you rather estimate mean and don't have the data, rather than having mean and not having data.)
Notice also that since you know $\tau_{Yi}$ values, then you really do not have to care about $\tau_{Xi}$ and $\tau_{Zi}$ values since they are somehow already included in the overall variances (the $\tau_{Yi}$'s), a thing that you also know in advance!
However, since your "data" here is something that is random than each time you called the "likelihood" function it would be using different data (two rnorm's). This seems to be rather a simulation study and using a standard optimizer on data that is not static does not seem to be a sound idea in here - and won't be more efficient. Also this is not a maximum likelihood scenario because likelihood is probability of data given some parameter and you don't have data (you generate it at random).
Comment: Maybe... But what I am suggesting is that probably your model is ill-specified in JAGS. Are you sure you did not meant `x[i] ~ dnorm(xhat[i], taux[i])` (i.e. "x comes from normal distribution with sd = taux and mean = xhat") ..? If you are conducting a simulation study this may be ok, but from your description is seems that you want to estimate something using some data, so it seems the model definition is wrong - maybe if it is corrected it could be estimated in `mle2`, `nlme` or `lme4`...
Comment: Ok, I see the point. Actually my data correspond to physical measurements, not random. But anyway from your comments I see that in that case applying a mle would be more complicated and probably less efficient than directly working with the mcmc. I thought it would be the contrary, but now I see that not here. I need to learn more about all these methods...! but thanks a lot for your reply, it helps me.


How to install lz4 shared libraries in rpm based machines?

Question: I tried to install lz4 libraries in CentOS, but failing with the following error.
[Bash]# yum install liblz4 liblz4-devel
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: centos.excellmedia.net
 * extras: centos.excellmedia.net
 * updates: centos.excellmedia.net
No package liblz4 available.
No package liblz4-devel available.
Error: Nothing to do
[Bash]#

What is the right way to setup the liblz4.so?
Also, I am not able to find the lz4 rpm packages for CentOS, but available for other rpm based Operating Systems. Is lz4 supported for CentOS?
Answer: On CentOS 6.x or 7.x, you can do the following to easily install lz4 using the package manager.
As root (sudo su - is your friend, or just preface each yum invocation below with sudo):
First, enable EPEL, as @Etan Reisner suggested:
yum install epel-release

Then, to install lz4 binary:
yum install lz4

And/or, to install the library:
yum install lz4-devel

And, you're done.
Comment: May be needed: `yum install lz4 --enable-repo=epel`
Answer: Packages for CentOS for lz4 appear to be available in EPEL.
Comment: You add the `EPEL` repo to your system and `yum` will work just fine. Information about doing that is on the page I linked.
Comment: I could find the required rpm and able to install it through above info. But how to make yum install it automatically. This is required for a naive user.


How do you disable strict errors for PHP 5.4 & 5.5 in Drupal 7 via the settings.php file?

Question: Currently I disabled the error messages via UI under Administration > Developement > Logging & Errors:

But I want to be able to do this via the settings.php file. How can that be done?
Answer: And the Drupal way:
Add this line in settings.php
$conf['error_level'] = 0; // 0: None, 1: Errors and warnings, 2: All messages

You can set any persistent variable this way. You can get the variable names simply using Firebug or any browser's developer tool and check the name attribute of the input field in the administration form. Which in you case the Logging & Errors page. If you look at the source code, the radio buttons have the name attribute error_level and each radio button has a value attribute. Simply set this value to the variable in you settings.php using $conf array.
You can also set any variable using variable_set() function in any custom module, in an update script for example.
Or using Drush:
drush vset error_level 0

Comment: I set the error_level to: 1 (errors & warnings) ... and those strict errors arent showing now even after a clear cache. But now the question is what does "errors & warnings" encompass? I dont want to supress other errors that i might want to know about.
Comment: If I look at the code of [`error_displayable()`](https://api.drupal.org/api/drupal/includes%21errors.inc/function/error_displayable/7) function, I see that if you set it to 1, it will display only the errors whose types are NOT 'Notice' AND NOT 'Strict warning'.
Answer: You can use this code on your settings.php file.
error_reporting(E_ALL ^ E_STRICT);

or
error_reporting(E_ALL & ~E_STRICT);

Note: E_STRICT has only been part of E_ALL since php 5.4
http://php.net/manual/function.error-reporting.php
Answer: Check if adding the following at the begging of the settings.php works:
ini_set('error_reporting', 30711);

that will affect the PHP settings for your site.
Comment: does that only disable the "strict errors" and nothing else? Where did you find that 30711 code? thanks.
Comment: In PHP, all the predefined constants from that are for error handling have a numeric value assigned. You can find more info at: http://stackoverflow.com/questions/3758418/numeric-values-of-error-reporting-levels#3758466
Answer: Drupal seems to override the setting.php
The best answers I found so far are the ones in that thread: Remove the strict warnings from being shown
Either you go hide the warnings in drupal interface:
This will hide everything, but occuring problems will still be logged, and hiding might be a good solution for a production environment anyway.
Or you directly modify the Drupal error handler:
 This is the solution I chose, which is more focused on the need.  The only drawback I see is that the operation might have to be done again when updating drupal version.
btw, avoid using lines like ini_set('error_reporting', 30711);
 error and warning values use mask for readibility, so, you'd better use something like (E_ALL & ~E_STRICT & ~E_WARNING) than any crude number value


How to set color for table rows based on column value in jQuery data table

Question: I am using jQuery datatables.I have the data like as follows
Column1 Column2 Column3
-----------------------
 AAA    BBB     CCC
 AAA    GGG     YYY
 BBB    ooo     LLL

Now in column1 for first 2 rows i have same value AAA.I want to apply some color to those
 rows.And then another color for third row.Like this i have 30 records.Is it possible to do this.If possible how i can do this.I am using jQuery data tables.Thanks in advance..
Answer: Use the fnRowCallback (or newer rowCallback) to achieve this
$('#example').dataTable({
    "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
        switch(aData[0]){
            case 'AAAA':
                $(nRow).css('color', 'red')
                break;
            case 'BBBB':
                $(nRow).css('color', 'green')
                break;
            case 'CCCC':
                $(nRow).css('color', 'blue')
                break;
        }
    }
});

Demo: Fiddle
Comment: Thanks @ArunPJohny , but I think ```return nRow ``` is required in this callback function, otherwise, datatable will raise an exception like " a node was not return by fnRowCallBack"
Comment: Please consider updating you answer as per @Herve MARTIN answer. The new API access the column data by name.
Comment: @PSR any way there should be some bussiness logic which says what color to be applied for a value
Answer: API has recently changed, you should now use aData['Column1'] instead of aData[0]
Answer: createdRow functionality was added in v 1.10

This callback is executed when a TR element is created (and all TD child elements have been inserted), or registered if using a DOM source, allowing manipulation of the TR element.
This is particularly useful when using deferred rendering (deferRender) or server-side processing (serverSide) so you can add events, class name information or otherwise format the row when it is created.

https://datatables.net/reference/option/createdRow
Example:
    $('#example').dataTable({
        // ...
        "createdRow": function( row, data, dataIndex ) {
            if ( data["column_index"] == "column_value" ) {
                $( row ).css( "background-color", "Orange" );
                $( row ).addClass( "warning" );
            }
        },
        // ...
    });

Comment: this worked for me but I had to specify "column_index" as the column name (string) rather than the index (int).


Comparing Cohen's D and Arcsine Transformation in Meta-analysis

Question: I'm conducting a meta-analysis that consists of 15 studies and ~40 construct effect sizes. Most of the constructs and studies use continuous variables and were straightforward in finding standardized mean differences (cohen's d). However, two studies use dichotomous variables and report the % of subjects who reported the construct. According to Lipsey & Wilson (2001), I'm leaning toward using the arcsine transformation over logit or probit due to the small sample size. For the below information "reported CSE", I got the arcsine transformations 1.115 for 28% and .644 for 10% according to the table of arcsine transformations in Lipsey & Wilson, making the estimated effect size = .471.
My main question is how do I compare the estimated effect sizes to the cohen's d in a forest plot? I have 95% CIs for all of the cohen's d calculations but only have the one point for the transformed ESs.
Comment: I feel that a point of clarification is needed here, since it will lead to potential misunderstandings (this not in any sense the fault of the poster of the question, however). It is disturbingly common to see many authors (presumably mostly not mathematicians) write "arcsin transformation" when referring to the *arcsine square root transformation*, $\sin^{-1}(\sqrt{p})$ or sometimes $2\sin^{-1}(\sqrt{p})$, which in either form  is the (asymptotic) variance-stabilizing transformation of a binomial proportion. ... ctd
Comment: ctd ... Dropping the square root  (and the $2$ if present) will mislead people who take the term at face value. In this case the term "arcsine" in Lipsey and Wilson (which they get in turn from Jacob Cohen's book on power) is actually referring to the second version of the transformation: $2\sin^{-1}(\sqrt{p})$ (the "$2$" giving us variance $1/n$ asymptotically). While this contains an arcsine, to merely call it "*arcsine*" very likely to lead to confusion. Consequently, this comment is to explicitly draw attention to the transformation that is actually being used in the reference.
Comment: At one point you mention "Lipskey". Do you mean *Lipsey* there?
Comment: Thank you for the clarification. I was referring to the arcsine square root transformation from Lipsey and Wilson. With that in my mind, do you have any information on how I would compare the transformed effect size without CIs to the others?
Answer: According to Introduction to Meta-Analysis, 2nd Edition (Borenstein, 2009) you can calculate the variance of d as follows (Chapter 4, page 27):

Once you have the variance, you can easily get the confidence intervals based on it.


Let $a$,$b$,$c$ be real numbers.

Question: Let $a$,$b$,$c$ be real numbers such that $a^{4}+b^{4}+c^{4}\leq27$. Prove the inequality:
$$\sum_{cyc}\frac{1}{\sqrt{a^{2}+1}}\geq \frac{3}{2}$$
My solution:
$\frac{1}{\sqrt{a^{2}+1}}\geq\frac{1}{2}\implies a^{2}\leq 3\implies a^{4}\leq 9$.
Similary : $b^{4}\leq9$ and $c^{4}\leq9$.
Then $a^{4}+b^{4}+c^{4}\leq27$ which is obviously true.
My solution is good ?
Comment: Also note that it isn't necessarily true that all three of $a^4, b^4, c^4$ are less than or equal to $9$. Given $x+y+z \ge \frac32$, we can conclude that *at least* one of $x,y,z$ is greater than or equal to $\frac12$, but not that all of them are.
Comment: The problem in your solution is that $a^4+b^4+c^4\leq 27$ doesn't mean $a^4\leq 9, b^4\leq 9, c^4\leq 9$. Take $a=b=1.8, c=1$, for example.
Comment: Looks like you're on the right track, but you're working backwards. Your goal should be to end with the cyclic sum, not to end with the given constraint.
Comment: You also don't know that $\frac1{\sqrt{a^2+1}}\ge \frac12$, only that the sum is greater than $\frac32$
Comment: any solution ??
Answer: Let $x_a = \sqrt{a^2 + 1}$, and similarly for $x_b$ and $x_c$. Then by RMS-HM inequality on $x_a, x_b, x_c$,
$$\begin{align*}
\frac{3}{x_a^{-1}+x_b^{-1}+x_c^{-1}}
&\le \sqrt{\frac{x_a^2+x_b^2+x_c^2}3} \\
&= \sqrt{\frac{(a^2+1)+(b^2+1)+(c^2+1)}3}\\
&= \sqrt{\frac{a^2+b^2+c^2}3 + 1}\tag1\\
\end{align*}$$
By RMS-AM inequality on $a^2,b^2,c^2$,
$$\begin{align*}
\frac{a^2+b^2+c^2}3
&\le \sqrt{\frac{a^4+b^4+c^4}3}\\
&\le \sqrt{\frac{27}3}\\
&= 3\tag2\\
\end{align*}$$
Combining results $(1)$ and $(2)$,
$$\begin{align*}
\frac{3}{x_a^{-1}+x_b^{-1}+x_c^{-1}} &\le \sqrt{3+1}\\
\sum \frac{1}{\sqrt{a^2+1}} &\ge \frac{3}{2}\\
\end{align*}$$

Let $a$,$b$,$c$ be real numbers.


Find the dimension of the system with equations and variables

Question: I'm studying dimension of the group now. But I have some trouble with it.
Consider the dimension of 2 by 2 orthogonal group, $O(2)=\{A \in M(2,2,R): AA^T=I\}$
Let $A = \begin{bmatrix}
a & b \\
c & d
\end{bmatrix}$.
Then by some calculation, we get three equations $a^2+b^2=1 , c^2+d^2=1, ac+bd=0$.
So, can I say that 'there was four free variable, and three equations, now it's dimesion is one although equations are not linear equation'?
Thank you
Comment: Hey. If you feel like your question has been anwsered, please accept the given answer. If not, please explain why and how it can be answered better and/or ask a follow up question, linking it here.
Answer: Yes and no.
You need to show that your system of equations does not include "repetition".
Suppose your conditions $A \in G \iff \phi(A)=0$, where $\phi : M_n(\mathbb{R}) \rightarrow \mathbb{R}^p$ (i.e. "there are $p$ condition equations).
In order to be able to conclude that the dimension of $G$ as a Lie group is $n^2 - p$, you need to show that $\phi$ is locally surjective. This translates to a condition on the rank of the differential of $\phi$.
More specifically, your conditions could be rephrased as $\phi(A) = AA^T-I = 0$, which is $n^2$ equations (in your case, n=2). However, only $n(n+1)/2$ of those are "free equations", namely the rank of the differential of $\phi$ is only  $n(n+1)/2$, and the dimension of $O(n)$ is $n(n-1)/2$.
Comment: Thank you sir. Why those n(n+1)/2 of equations are free equations?, what's the exact definition of free equations?
Comment: 1/ Because the rank of the differential of $\phi$ is $n(n+1)/2$.
2/ A set of free equations is a relationship$\phi(X) = 0$ where the differential of $\phi$ has full rank everywhere. Intuitively, "no equation can be deduced from the others".


Resolução de equações

Question: Criei um plano cartesiano e eu recebo do usuário uma equação qualquer. O meu objetivo é traçar essa equação no plano. Estou fazendo isso usando canvas. Só que aqui entra um problema, eu só consigo fazer isso se eu separar cada tipo de equação (círculo, reta...).
O que eu quero é que o usuário coloque qualquer tipo de equação, de 2 variáveis (x e y). Então eu pego e coloco no 'x' valores de -100 a 100. Ou seja, no final das contas, eu vou ter uma equação com uma só variável (y). Só que eu não consigo generalizar essas equações.
Como eu faço um algoritmo para que independente da forma que o usuário insira, eu isole o 'y' da equação?
Exemplo 1: 3²/4 + y²/25 = 1
Exemplo 2: 4² - 6 = (y-1)²
Ele deve retornar qual o valor de y.
Quem puder ajudar, agradeço!
//JAVASCRIPT, mas aceito PHP ou C++ para ter como base para eu fazer em javascript (sem usar framework em nenhuma dessas)
Comment: Não existe um meio generalizado de se resolver equações do 3º grau para cima. Seus exemplos são ambos de 2º grau, então a [Fórmula de Bhaskara](http://pt.wikipedia.org/wiki/F%C3%B3rmula_de_Bhaskara#F.C3.B3rmula) é suficiente. Nesse caso, é só uma questão de interpretar e manipular a entrada do usuário. Em que formato será feita essa entrada? Pode dar alguns exemplos? (tipo, como o usuário entraria com "a elevado a b"?)
Comment: Bastante relevante para o problema: http://www.cs.cornell.edu/w8/~andru/relplot/
Eu entrei em contato solicitando o código fonte. Caso aceito, postarei uma resposta com explicações sobre como a mesma coisa pode ser feita com equações gerais e não apenas polinômios.
Comment: ele vai entrar da seguinte forma: x^2 + 3*y^2 = 12. Sendo que o 'x' eu vou preencher no programa com valores de -100 a 100. Tem um site que faz exatamente o que eu quero: http://www.wolframalpha.com/
Comment: Cara, comparar o wolframalpha com qualquer coisa que nós pobres mortais somos capaz de fazer é sacanagem... :P
Answer: Se após a substituição do x o resultado for uma equação até de 2º grau em y, então dá para resolver pela Fórmula de Bhaskara:
a*y^2 + b*y + c = 0

y = -b +- raiz(b^2 - 4*a*c)
    -----------------------
              2*a

O mais complicado é interpretar a entrada... Você precisa convertê-la do formato texto para um formato abstrato, que te permita manipular os elementos (fazer a multiplicação de termos, jogar termos da direita pra esquerda com sinal invertido, etc) até isolar a, b e c.
O ideal é usar algum parser capaz de interpretar o seu formato desejado. Nessa resposta, vou dar uma sugestão bem simples, sem tratamento de erro nem nada, mas ainda capaz de interpretar equações simples como seu exemplo.
Análise léxica
Basicamente, uma sequência de dígitos é um número (que chamarei de c - "constante"), x é x e y é y. Espaços são ignorados, e os operadores são =, + e -, * e / e ^, nessa ordem de precedência. Parênteses são um caso especial...
  function token() {
    var numero = /^-?\d+/.exec(s);
    if ( numero ) {
      s = s.substring(numero[0].length);
      return variavel(0,0,parseInt(numero[0], 10)); // x^0 * y^0 * c
    }

    var simbolo = s[0];
    s = s.substring(1);

    if ( simbolo == "x" )
      return variavel(1); // x^1 * y^0 * 1
    if ( simbolo == "y" )
      return variavel(0,1); // x^0 * y^1 * 1
    if ( operadores[simbolo] )
        return operadores[simbolo]();
    if ( simbolo == " " )
      return token(); // Ignora o espaço em branco

    throw "Formato inválido";
  }

Separar o texto em tokens é simplesmente o caso de ir retirando tokens do começo da string até ela acabar:
function lexica(str) {
  var s = str;
  function token() { ... }

  var tokens = [];
  while ( s.length > 0 )
    tokens.push(token());
  return tokens;
}

Análise sintática
Assumindo que variáveis e constantes têm precedência 0, exponenciação 1, multiplicação 2, etc (i.e. a*b^c é a*(b^c) e não (a*b)^c), se lermos os tokens da esquerda pra direita, e decidirmos quem entra dentro de quem, vamos terminar com uma árvore de expressões e sub-expressões:
function sintatica(tokens, i) {
  var ret = undefined;
  i = i || 0;
  for ( ; i < tokens.length ; i++ ) {
    if ( !ret || tokens[i].p >= ret.p ) { // Se o próximo da lista tem precedência maior
      tokens[i].esq = ret;                // a antiga raiz entra na esquerda dele
      ret = tokens[i];                    // e ele vira a raiz
    }
    else { // Senão, ele é inserido na direita da raiz

      // mas se alguém na direita tem precedência menor que ele
      for (var pai = ret ; pai.dir && pai.dir.p > tokens[i].p ; pai = pai.dir) { }
      tokens[i].esq = tokens[i].esq || pai.dir; // então ele o coloca à sua esquerda
      pai.dir = tokens[i];                      // e toma o lugar dele
    }
  }
  return [ret,i]; // No fim todos os tokens vão formar uma árvore, na precedência certa
}

Esse é um método bem simplório de se interpretar uma expressão. Ele associa à direita sempre, enquanto nem toda operação faz isso (a potência, por exemplo, associa à esquerda). De novo, sugiro procurar um parser mais completo para tornar seu sistema mais robusto.
Dito isso, vamos finalmente ao foco da pergunta:
Substituindo o x
No código acima, eu defini um tipo variavel que é implementado da seguinte forma:
function variavel(x,y,c) {
  return {
    x:x || 0, // O expoente do x, padrão 0
    y:y || 0, // O expoente do y, padrão 0
    c:c || 1, // Uma constante, padrão 1
    p:0,      // A precedência é zero (não é um operador)

Ou seja, x vira {x:1,y:0,c:1}, y^2 vira {x:0,y:2,c:1} e 42 vira {x:0,y:0,c:42}. Também dá pra misturar, tipo 42*x*y^2 vira {x:1,y:2,c:42 }, desde que o x não passe de 1 (nessa representação; na entrada do usuário, pode). Representando as variáveis dessa forma, substituir o x por um valor é simplesmente uma questão de multiplicar o c pelo valor de x, e passar o xde 1 para 0 (se não havia x, o valor não se altera).
    substitui:function(x) { this.c *= this.x ? x : 1; this.x = 0; },

Nos operadores, basta substituir independentemente o lado esquerdo e o lado direito...
function substitui(x) {
  this.esq.substitui(x);
  this.dir.substitui(x);
}

Achando as parcelas
Depois de substituir o x, queremos achar as parcelas e passar todas elas pra esquerda. Em alguns casos é fácil:
x^2 + 3*y^2 = 12 [x = 2]
==>
4 + 3*y^2 + (-12) = 0

Em outros é mais difícil:
(y + 1)^2
==>
(y + 1)*(y + 1)
(y*y) + (y*1) + (1*y) + (1*1)

A solução é fazer com que cada operador tenha um função pra transformar seus argumentos numa lista de parcelas. Uma variável/constante é uma parcela só:
function variavel(x,y,c) {
    soma:function() { return [this]; },

Uma soma é simplesmente o conjunto das parcelas da esquerda mais o conjunto das parcelas da direita:
var operadores = {
  "+":function(){
    return {
      p:3,
      substitui:substitui,
      soma:function() { return this.esq.soma().concat(this.dir.soma()); }
    };
  },

A multiplicação tem que multiplicar cada parcela da esquerda com cada parcela da direita:
  "*":function(){
    return {
      p:2,
      substitui:substitui,
      soma:function() {
        var esq = this.esq.soma(); // Acha todas as parcelas da esquerda
        var dir = this.dir.soma(); // e todas da direita

        var ret = [];
        for ( var i = 0 ; i < esq.length ; i++ )   // Cada termo da esquerda
          for ( var t = 0 ; t < dir.length ; t++ ) // Vezes cada termo da direita
            ret.push(esq[i].vezes(dir[t]));
        return ret;
      }
    };
  },

Etc. Por fim, o operador = têm de passar tudo que está na direita pra esquerda, com o sinal invertido:
  "=":function(){
    return {
      p:4,
      substitui:substitui,
      soma:function() {
        var ret = this.esq.soma();
        var dir = this.dir.soma();
        for ( var i = 0 ; i < dir.length ; i++ ) {
          dir[i].c = -dir[i].c; // Passa pra esquerda, trocando o sinal
          ret.push(dir[i]);
        }
        return ret;
      }
    };
  },

Bhaskara
Por fim, é só aplicar a fórmula mencionada no início da resposta:
function avaliar(equacao, x) {
  var raiz = sintatica(lexica(equacao))[0]; // Interpreta
  raiz.substitui(x); // Substitui o x
  var termos = raiz.soma(); // Transforma numa soma de termos

  // Bhaskara
  var a = 0, b = 0, c = 0;
  for ( var i = 0 ; i < termos.length ; i++ ) {
    if ( termos[i].y == 0 )
      c += termos[i].c;
    else if ( termos[i].y == 1 )
      b += termos[i].c;
    else if ( termos[i].y == 2 )
      a += termos[i].c;
    else
      throw "Equação maior que segundo grau!";
  }

  if ( a == 0 )
    return [-c/b] // Equação de primeiro grau

  var rdelta = Math.sqrt(b*b - 4*a*c);
  return [(-b + rdelta)/(2*a), (-b - rdelta)/(2*a)];
}

Exemplo completo:
Juntando o código acima, com uma ou outra coisinha a mais (os operadores que faltavam, e os parênteses - que têm precedência zero, mas "capturam" tudo o que tem dentro deles), temos um exemplo completo:

function substitui(x) {
  this.esq.substitui(x);
  this.dir.substitui(x);
}

var operadores = {
  "=":function(){
    return {
      p:4,
      substitui:substitui,
      soma:function() {
        var ret = this.esq.soma();
        var dir = this.dir.soma();
        for ( var i = 0 ; i < dir.length ; i++ ) {
          dir[i].c = -dir[i].c; // Passa pra esquerda, trocando o sinal
          ret.push(dir[i]);
        }
        return ret;
      }
    };
  },
  "+":function(){
    return {
      p:3,
      substitui:substitui,
      soma:function() { return this.esq.soma().concat(this.dir.soma()); }
    };
  },
  "-":function(){
    return {
      p:3,
      substitui:substitui,
      soma:function() {
        var ret = this.esq.soma();
        var dir = this.dir.soma();
        for ( var i = 0 ; i < dir.length ; i++ ) {
          dir[i].c = -dir[i].c; // troca o sinal
          ret.push(dir[i]);
        }
        return ret;
      }
    };
  },
  "*":function(){
    return {
      p:2,
      substitui:substitui,
      soma:function() {
        var esq = this.esq.soma();
        var dir = this.dir.soma();
        var ret = [];
        for ( var i = 0 ; i < esq.length ; i++ )   // Cada termo da esquerda
          for ( var t = 0 ; t < dir.length ; t++ ) // Vezes cada termo da direita
            ret.push(esq[i].vezes(dir[t]));
        return ret;
      }
    };
  },
  "/":function(){
    return {
      p:1,
      substitui:substitui,
      soma:function() {
        var ret = this.esq.soma();
        var divisor = this.dir.c;
        for ( var i = 0 ; i < ret.length ; i++ )
            ret[i].c /= divisor;
        return ret;
      }
    };
  },
  "^":function(){
    return {
      p:1,
      substitui:substitui,
      soma:function() {
        var esq = this.esq.soma();
        var potencia = this.dir.c;
        var ret = esq;
        while ( potencia-- > 1 ) { // Cada termo da esquerda multiplica com
          var tmp = ret;           // cada termo da esquerda n vezes
          var ret = [];
          for ( var i = 0 ; i < tmp.length ; i++ )
            for ( var t = 0 ; t < esq.length ; t++ )
              ret.push(tmp[i].vezes(esq[t]));
        }
        return ret;
      }
    };
  },
  "(":function(){
    return { par:"(" };
  },
  ")":function(){
    return { par:")" };
  }
};

function variavel(x,y,c) {
  return {
    x:x || 0,
    y:y || 0,
    c:c || 1,
    p:0,
    substitui:function(x) { this.c *= this.x ? x : 1; this.x = 0; },
    soma:function() { return [this]; },
    vezes:function(v) {
      return variavel(this.x + v.x, this.y + v.y, this.c * v.c);
    }
  }
}

function lexica(str) {
  var s = str;

  function token() {
    var numero = /^\d+/.exec(s);
    if ( numero ) {
      s = s.substring(numero[0].length);
      return variavel(0,0,parseInt(numero[0], 10));
    }

    var simbolo = s[0];
    s = s.substring(1);

    if ( simbolo == "x" )
      return variavel(1);
    if ( simbolo == "y" )
      return variavel(0,1);
    if ( operadores[simbolo] )
        return operadores[simbolo]();
    if ( simbolo == " " )
      return token(); // Ignora o espaço em branco

    throw "Formato inválido";
  }

  var tokens = [];
  while ( s.length > 0 )
    tokens.push(token());
  return tokens;
}

function sintatica(tokens, i) {
  var ret = undefined;
  i = i || 0;
  for ( ; i < tokens.length ; i++ ) {
    // Parênteses "quebram" a lógica da precedência...
    if ( tokens[i].par == ')' )
        break;
    if ( tokens[i].par == '(' ) {
        var conteudo = sintatica(tokens, i+1);
        i = conteudo[1];
        tokens[i] = conteudo[0];
        tokens[i].p = 0;
    }
    //

    if ( !ret || tokens[i].p >= ret.p ) {
      tokens[i].esq = ret;
      ret = tokens[i];
    }
    else {
      for (var pai = ret ; pai.dir && pai.dir.p > tokens[i].p ; pai = pai.dir) { }
      tokens[i].esq = tokens[i].esq || pai.dir;
      pai.dir = tokens[i];
    }
  }
  return [ret,i];
}

function avaliar(equacao, x) {
  var raiz = sintatica(lexica(equacao))[0]; // Interpreta
  raiz.substitui(x); // Substitui o x
  var termos = raiz.soma(); // Transforma numa soma de termos

  // Bhaskara
  var a = 0, b = 0, c = 0;
  for ( var i = 0 ; i < termos.length ; i++ ) {
    if ( termos[i].y == 0 )
      c += termos[i].c;
    else if ( termos[i].y == 1 )
      b += termos[i].c;
    else if ( termos[i].y == 2 )
      a += termos[i].c;
    else
      throw "Equação maior que segundo grau!";
  }

  if ( a == 0 )
    return [-c/b] // Equação de primeiro grau

  var rdelta = Math.sqrt(b*b - 4*a*c);
  return [(-b + rdelta)/(2*a), (-b - rdelta)/(2*a)];
}

/***** Exemplos *****/

var exemplos = ["x^2 + 3*y^2 = 12",
                "x^2/4 + y^2/25 = 1",
                "x^2 - 6 = (y-1)^2",
                "4*x + 5*y = 15"];

for ( var e = 0 ; e < exemplos.length ; e++ ) {

  var entrada = exemplos[e];

  document.body.innerHTML += "
" + entrada + "
";

  for ( var i = 0 ; i <= 5 ; i++ )
    document.body.innerHTML += "
x = " + i + ", y = " +
                                   JSON.stringify(avaliar(entrada,i)) +
                               "
";
}

Como já dito, esse parser feito à mão não é muito bom, mas deve ser suficiente pra demonstrar a lógica necessária pra tratar das equações, que é o foco da pergunta.
Deixo a correção do bug quando x == 0 como exercício para o leitor... :P
Comment: Eu tinha começado esboçar algo do tipo, mas vi que daria muito trabalho fazer tudo que eu queria.. Quando pensei no problema achei que seria mais simples, tanto que já to com o traçador e o gráfico prontos (só falta interpretar a equação) rsrs. No mais, valeu pela ajuda!

Resolução de equações


Optional generic parameter type narrowing

Question: I have the following function that takes an optional argument of type T (a generic).
function test<T>(foo?: T) {
  return foo;
}

const result = test("bar");
// result is type "bar" | undefined

const result2 = test();
// result2 is type "unknown"

How can I correctly type this function such that the compiler will be able to tell that the result variable is of type "bar" and not type "bar" | undefined and additionally the result2 variable is of type undefined and not type unknown?
Comment: If you're sure result can never be undefined, write `const result = test("bar")!;`
Comment: Thanks! I'd like it to be inferred, if possible.
Answer: I would do this with function overloading:
function test<T>(foo: T): T;
function test<T>(foo?: undefined): undefined;
function test<T>(foo?: T): T | undefined {
    return foo;
}

const result = test("bar"); // result is of type "bar"

const result2 = test(); //result2 is of type undefined

Playground link
Answer: You can use overload signatures to achieve this:
function test(): undefined;
function test<T>(foo: T): T;
function test<T>(foo?: T) {
  return foo;
}

const result = test("bar");
// result is type "bar"

const result2 = test();
// result2 is type undefined

Playground Link
Comment: Perfect... other response was a few seconds before you so I gave them the check but I really appreciate it!
Answer: The reason it is "bar" | undefined is because you are setting foo to an optional parameter. Optional parameters are the same as undefined in JS so using foo?: T will automatically append a | undefined to the type.
Comment: Thanks! I'm trying to see if I can achieve type narrowing within the function that will allow the return value to be inferred based on whether a parameter is provided or not.
Comment: @Nick Are you talking about having `undefined` as the default return value then? That can be done if you have ``. I can update my answer too.


URL format - Action before or after object?

Question: I'm building a web application and I came across one question. Here at the office we have divided opinions so I thought it would be nice to share the question and listen to the very best (you!)
When creating the URLs for the application I was thinking about using the standard format:
For the index of the Clients module it would be:
www.example.com/clients

Then, for the details page of a client it would be:
www.example.com/clients/[client_slug]

And finally, for the update page of a client it would be:
www.example.com/clients/[client_slug]/update

Here comes the question:
What about changing the order of the parameters in the URL for the update page? It would look like this:
www.example.com/clients/update/[client_slug]

(Keeping the [client_slug] parameter always at the end of the URL.)
For me it makes more sense, since the URL will always follow the format "domain/module/function/object" but for some guys here it doesn't.
What you guys think? Any SEO implications or is it just a personal choice?
Answer: I don't think there is any implication in terms of ranking. If object is what people would search for, then having it early in the url like

www.example.com/clients/[client_slug]/update

is what i would prefer because of the way the url would show up in organic results under the title.


Can I take part in the Olympic Games decathlon by default?

Question: I'm 17 and I do the decathlon in high school. My country has a terrible sports history and doesn't have a record for half of the athletics events.
Their record for decathlon was set in 1979 and is only 6412 points. I am close to this number and I wonder: can an athlete take part in the Olympic Games for their country even while being worse than the average athlete at the Olympics?
By the time the next Olympics start I should be at around 7000 points and I want (like every other athlete) to go to the Olympics. I've seen cases of athletes put in just because the country had nobody else.
Comment: Depending on the average used, half and as much as three-quarters of the actual Olympic athletes could be "worse than the average athlete". Nor is this very relevant to most (or all) qualification standards to the Olympics.
Comment: Related: [How can a country be eligible to participate in any sport in the Olympics?](http://sports.stackexchange.com/q/3942/1723)
Answer: Entry by Qualifying Standards
In decathlon, your ability compared to the average athlete is irrelevant; what you need is to meet the fixed entry standard within a certain time period prior to the Games, for yourself.
The entry standard for the 2016 Olympic Games decathlon event was

a score of 8100 or greater,

obtained at an IAAF-approved event

between 1 January 2015 and 11 July 2016

This gave about 18 months of events, and qualifying ended four weeks prior to the opening ceremony.
A similar standard and qualifying period can be expected for the 2020 Olympic Games; you could need a score notably above 8000 at an IAAF event during 2019 or 2020 prior to the Games' beginning.
Entry by Nomination
If you do not meet this standard, there may be one "saving grace" method of entry.
For the 2016 Olympic Games, contrary to prior Games, a National Olympic Committee which did not have any athletes meet the entry standards in one of a list of specific events, could nominate a single athlete (in each sex, as and where appropriate) for those specific events not qualified to. This guaranteed every NOC could have at least one athlete in every event of the athletics components of the games, if they so desired.
It is not known whether this alternative method of entry will be in place for Tokyo 2020 Olympic Games. You should aim to learn and to meet the full qualifying standard regardless of whether others in your country can or cannot also meet it.
Comment: This looks like an excellent answer. Could you add some sources to cite?
Comment: To add to this: In athletics, the "entry by nomination" is usually in events which can handle arbitrarily large numbers of athletes, e.g. the 100m (which has a preliminary round with dozens of athletes) or the marathon (on a road course where field-size variation of a few dozen athletes won't affect the competition). The relative size of the decathlon field, however, has a direct and significant effect on the competition schedule, and consequently entry by nomination in this event might be more difficult.
Comment: To be more specific about my above point: for the 2017 IAAF World Championships in London, the route you've described as "entry by nomination" specifically excepts the combined events (heptathlon/decathlon) from entry this way. Decathletes hoping to compete in London need to beat the standard. I expect this will be true of Tokyo as well. If you can get entry by nomination, it won't be in the decathlon.


Convert pot file to array

Question: So, what I need to do is to convert the contents of a .pot file to an array.
I know there is _locale_import_read_po(), but when I tried using it with the following code, I got an error.
$translations = _locale_import_read_po('db-store', 'translations.pot');

The translations.pot file is located in the same directory containing the file with the code I wrote.
The error I get with dpm($translations) is the following one.

Error message
The translation import failed, because the file could not be read.

This is the code from the _locale_import_read_po() function, that outputs that error.
// The file will get closed by PHP on returning from this function.
$fd = fopen($file->uri, 'rb');
if (!$fd) {
  _locale_import_message('The translation import failed, because the file %filename could not be read.', $file);
  return FALSE;
}

As far as I know the 'rb' file open mode is to open non-text files.
Why do I get that error message?
Answer: The second parameter of _locale_import_read_po() is a Drupal file object, which in Drupal 7 is simply a stdClass object.
The quicker way to use that function and pass a file object as it expects is using code similar to the following one.
 $po_files = file_scan_directory('./', '^translation\.po$', array('recurse' => FALSE));
 if (count($po_files)) {
   require_once DRUPAL_ROOT . '/includes/locale.inc';
   $po_file = reset($po_files);
   _locale_import_read_po('db-store', $po_file);
 }

Notice that the second argument of file_scan_directory() is a preg_match() regular expression, not a filename.
Using that code, you don't need to save the file using the file API, which is essentially useless, since there is already a file in the file system.
file_save() stores the file in the database; for a file that is used for a single operation like that, it's not necessary.
I used st() as example for writing the code I shown in this answer.
You could also make the code I show more generic to accept a regular expression and a directory name, and use it to load more files.
function mymodule_import_translations($directory, $pattern) {
  $po_files = file_scan_directory($directory, $pattern, array('recurse' => FALSE));
  if (count($po_files)) {
    require_once DRUPAL_ROOT . '/includes/locale.inc';
    foreach ($po_files as $po_file) {
      _locale_import_read_po('db-store', $po_file);
    }
  }
}

Answer: The $file parameter needs to be a Drupal file object corresponding to the PO file to import.
You can create one using the file_save function:
$file = file_save((object)array(
    'filename' => $file_name,
    'uri' => $destination_uri,
    'status' => FILE_STATUS_PERMANENT,
    'filemime' => file_get_mimetype($destination_uri),
  ));

Convert pot file to array