Q:

Approx quantile on a array of doubles - Spark dataframe

I have a spark dataframe defined as:
+----------------+--------------------+-----------+
|        id      |            amt_list|ct_tran_amt|
+----------------+--------------------+-----------+
|1               |[2.99, 7.73, 193....|         23|
|2               |[9.99, 9.95, 5.0,...|         17|
|3               |[4.57, 14.06, 0.7...|         19|

How do I calculate approximate quantile (1st and 3rd) as new columns?
df.stat.approxQuantile("amt",Array(0.25,0.75), 0.001) does not take a wrapped array as input.

A:

I'm not aware of a built-in spark function to do this, so I would go for an UDF:
def calcPercentile(perc:Double) = udf((xs:Seq[Double]) => xs.sorted.apply(((xs.size-1)*perc).toInt))

df
  .withColumn("QT1", calcPercentile(0.25)($"amt_list"))
  .withColumn("QT3", calcPercentile(0.75)($"amt_list"))
  .show()

EDIT:
There is also an approach without UDF:
df
  .withColumn("Q1", sort_array($"amt_list")(((size($"amt_list")-1)*0.25).cast("int")))
  .withColumn("Q3", sort_array($"amt_list")(((size($"amt_list")-1)*0.75).cast("int")))
  .show()


Q:

Compile less in TYPO3

I want to customize the styles of tx_news, so I copied news-basics.less and news-basics.css into my site extension.
My environment is: TYPO3 9.5, Boostrap_package 11.0.2, tx_news 7.3.1
Now my question is what is the best practice to compile news-basics.less into news-basics.css? As bootstrap_package seems to include a scss compiler (maybe a less compiler too ?-), it would be nice to use it.

A:

Yes, Bootstrap Package includes a LESS compiler (but will be removed in 12.x).
Add e.g. the following lines to your setup TS and Bootstrap Package will compile it:
page.includeCSS.news-basic = EXT:yoursitepackage/Resources/Public/Less/news-basic.less


Q:

WCF Configuration Reference

Where is the definitive .NET WCF configuration reference documentation?
I need some documentation that lists every configuration option, with helpful descriptions of what each setting actually means.

A:

This documentation should help you out a lot.
Windows Communication Foundation Configuration Schema
It lists all the possible configuration settings for Windows Communication Foundation (WCF).
The visualization of all the configuration settings allows for a unique perspective. At the end of the article, there are a few links which if you follow will bring you into expanded discussion and documentation. If you want to just reference any of the settings found in the schema, there will be an article on MSDN expanding on it - just search for it.


Q:

Regex works but not with preg_match_all

I can't get the following Regex to work in PHP. Basically I am trying to take some horrendous Outlook HTML that contains a numbered list, remove the HTML, then Regex the plain text to get the list.
If I take the text that is produced by strip_tags() and test it on regex101.com, it finds the ordered list just fine. If I use that same regex in preg_match_all in PHP it produces an empty array.
Fiddles and regex101 below:
PHP:
$calendar_code = '

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">

This is a test of the agenda and objectives format

This shouldn’t get picked up

Dasdasdasd d asda sd  : asd obe: sad neither shood this

Objective: This is how the object should look, this is a long one

Agenda:

1.
Make like a tree

2.
And

3.
Get out of here

Some more stuff here, and here and ::: ;s

Sadfdsf sdfdfeswrfew

';

$strip = strip_tags($calendar_code);

echo "
";
echo $strip;

preg_match_all("/^(\d+\.)\s+([^\r\n]+)(?:[\r\n]*)/m", $strip, $matches);
print_r($matches);

PHPFiddle: http://phpfiddle.org/main/code/ygut-5jj5
As you can see I echo out the HTML stripped text. When I put this text in to regex101.com it works perfectly. See here: https://regex101.com/r/wW1kC9/1
I thought it might have something to do with the line endings, but I replaced all the HTML line endings with \n before doing the strip_tags() and it still doesn't work.
Can anyone see why this regex is not working with preg_match_all()
UPDATE:
It's been pointed out that non-breaking spaces are the reason, so removing or allowing for them in the regex will fix it. However it has also been pointed out that as the format of these lists will be quite random depending on the email client that sends the list, some using
 and some not for example, regex will not work for every situation, or even the majority of situations.
I need a better way of getting the contents of lists created by any number of different email clients.
For some background, people create these lists in emails and send them to a special email account. My code then accesses these emails and retrieves the lists for use elsewhere in my app. As these list are being created in the many different email clients available, they will invariably have different(random) formatting applied. For example when you create lists in Outlook 2016, it adds
 and  tags with styling to create the list.

A:

You have to decode HTML entities:
$strip = html_entity_decode(strip_tags($calendar_code));

Then there is another tricky part that you should take care of: after this decoding a non-breaking space will turn into its hex representation 0xC2 0xA0 which is not matched by \s token anymore so you have to consider its Unicode code point 00a0 as well:
preg_match_all("/^(\d+\.)[\s\x{00a0}]+([^\r\n]+)(?:[\r\n]*)/mu", $strip, $matches);

Live demo


Q:

Set base paths for my files for webpack

When using requirejs, I do this:
require.config({
    baseUrl: "scripts"
});
console.log("Starting!");
require(["A", "B", "C"], mainFunction);

I was googling how to set the base path for Webpack. I found resolve.root. However that just doesn't work:
$ node run_with_node.js
D:\node_modules\webpack\lib\webpack.js:19
                throw new WebpackOptionsValidationError(webpackOptionsValidationErrors);
                ^

WebpackOptionsValidationError: Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
 - configuration.resolve has an unknown property 'root'. These properties are valid:
   object { alias?, aliasFields?, cachePredicate?, descriptionFiles?, enforceExtension?, enforceModuleExtension?, extensions?, fileSystem?, mainFields?, mainFiles?, moduleExtensions?, modules?, plugins?, resolver?, symlinks?, unsafeCache?, useSyncFileSystemCalls? }
    at webpack (D:\node_modules\webpack\lib\webpack.js:19:9)
    at Object.<anonymous> (D:\web\voxnap\run_with_node.js:9:18)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:425:7)
    at startup (bootstrap_node.js:146:9)

I also tried to use resolve.modules, but if I did that, Webpack couldn't find internal modules from babeljs.
Error: Cannot find module "babel-runtime/helpers/typeof"

So how to properly add a directory that contains my scripts?

A:

You've looked at the documentation for webpack 1. Webpack 2 removed resolve.root and unified it to resolve.modules, as shown in the Migration Guide of the official webpack 2+ docs.
The default value of resolve.modules is ["node_modules"] and if you want to keep the regular module resolution, you have to include it as well.
resolve: {
  modules: [
    path.resolve(__dirname, "scripts"),
    "node_modules"
  ]
}


Q:

How to make it look like a table with CSS?

Hello i got this code on my WordPress description that was inserted while uploading CSV file.
Now that text is not so nice too look at, i want to make it as a table but this code is not included with HTML table code.
Now i got this:

Weight:

82g

This shows Weight:
               82g
How can i make it look like this with css?

Who will help me get 1billion karma points! :)

A:

see if this can help you with only css, you just have to add one div

.fake-table {
    width: 200px;
}

.fake-table dt, .fake-table dd {
    background-color: #f5f5f5;
    width: 46%;
    float: left;
    margin: 0;
    padding: 4px 2%;
    border-bottom: 1px solid #fff;

    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.fake-table:after {
    content: "";
    display: table;
    clear: both;
}

.fake-table dt:hover, .fake-table dd:hover {
    background-color: #eee;
}

Weight:

82g

jfkldsf fjdklf fjdsklf fjdkslf fjdksl

82g

Weight:

82g


Q:

VS13 C++ greedy match not greedy enough

Consider the following code:
wstring String = L"HMM";
wsmatch Matches;
if( regex_match( String, Matches, wregex( L"(H+|M+)([^HM]*)(H+|M+)([^HM]*)(H+|M+)" ) ) )
    wcout << Matches[1].str() << L"-" << Matches[3].str() << L"-" << Matches[5].str() << endl;
else
    wcout << L"No match\n";

You would expect that the second M+ would greedily consume all M's and give us a No match as result. Instead we get H-M-M as result.
It looks that VS13 tries to maximise the number of submatches and sacrifices the greediness for that?

A:

You would expect that the second M+ would greedily consume all M's and give us a No match as result.

The behavior you're describing is possessive, not greedy.  A greedy quantifier consumes as much as it can originally, but then gives back as much as necessary to achieve an overall match.
In flavors that support them, possessive quantifiers are formed by adding a + after the normal quantifier.  This regex would behave the way you expect:
(H++|M++)([^HM]*+)(H++|M++)([^HM]*+)(H++|M++)

However, the flavor you're using doesn't support possessive quantifiers.  Atomic groups can be used for the same thing, but you don't have those either.


Q:

Looking for a variable inside another programs memory

I'm trying to find an int value inside another program's memory (my own little c++ that only holds that variable). The value is 1234, but I can't seem to find it.
This is what I'm doing:
for (uint adr = 0x00000000; adr <= 0x7FFFFFFF; adr += 1)
{
    // we want status...
    uint progress = (adr*100) / 0x7FFFFFFF;
    Console.WriteLine("Progress: {0} %", progress);

    // look for the int
    IntPtr bytes;
    byte[] buffer = new byte[4];

    ReadProcessMemory(process, adr, buffer, 4, out bytes);

    if (BitConverter.ToInt32(buffer, 0) == 1234)
    {
        // we found it...
    }
}

There are several errors:

The progress does not work.
I never get a hit on found.

Thanks

A:

Maybe this value 1234 is saved in
2 byte value not 4 byte.
Check readprocess memory not throw you error because you not OpenProcess.
Try big endian reading too, then try reverse bits before reading.


Q:

How exactly does Perl handle operator chaining?

So I have this bit of code that does not work:
print $userInput."\n" x $userInput2; #$userInput = string & $userInput2 is a integer

It prints it out once fine if the number is over 0 of course, but it doesn't print out the rest if the number is greater than 1. I come from a java background and I assume that it does the concatenation first, then the result will be what will multiply itself with the x operator. But of course that does not happen. Now it works when I do the following:
$userInput .= "\n";
print $userInput x $userInput2;

I am new to Perl so I'd like to understand exactly what goes on with chaining, and if I can even do so.

A:

You're asking about operator precedence.  ("Chaining" usually refers to chaining of method calls, e.g. $obj->foo->bar->baz.)
The Perl documentation page perlop starts off with a list of all the operators in order of precedence level.  x has the same precedence as other multiplication operators, and . has the same precedence as other addition operators, so of course x is evaluated first.  (i.e., it "has higher precedence" or "binds more tightly".)
As in Java you can resolve this with parentheses:
print(($userInput . "\n") x $userInput2);

Note that you need two pairs of parentheses here.  If you'd only used the inner parentheses, Perl would treat them as indicating the arguments to print, like this:
# THIS DOESN'T WORK
print($userInput . "\n") x $userInput2;

This would print the string once, then duplicate print's return value some number of times.  Putting space before the ( doesn't help since whitespace is generally optional and ignored.  In a way, this is another form of operator precedence: function calls bind more tightly than anything else.
If you really hate having more parentheses than strictly necessary, you can defeat Perl with the unary + operator:
print +($userInput . "\n") x $userInput2;

This separates the print from the (, so Perl knows the rest of the line is a single expression.  Unary + has no effect whatsoever; its primary use is exactly this sort of situation.

A:

This is due to precedence of . (concatenation) operator being less than the x operator. So it ends up with:
use strict;
use warnings;
my $userInput = "line";
my $userInput2 = 2;
print $userInput.("\n" x $userInput2);

And outputs:
line[newline]
[newline]

This is what you want:
print (($userInput."\n") x $userInput2);

This prints out:
line
line

A:

As has already been mentioned, this is a precedence issue, in that the repetition operator x has higher precedence than the concatenation operator .. However, that is not all that's going on here, and also, the issue itself comes from a bad solution.
First off, when you say
print (($foo . "\n") x $count);

What you are doing is changing the context of the repetition operator to list context.
(LIST) x $count

The above statement really means this (if $count == 3):
print ( $foo . "\n", $foo . "\n", $foo . "\n" );  # list with 3 elements

From perldoc perlop:

Binary "x" is the repetition operator. In scalar context or if the left operand is not enclosed in parentheses, it returns a string consisting of the left operand repeated the number of times specified by the right operand. In list context, if the left operand is enclosed in parentheses or is a list formed by qw/STRING/, it repeats the list. If the right operand is zero or negative, it returns an empty string or an empty list, depending on the context.

The solution works as intended because print takes list arguments. However, if you had something else that takes scalar arguments, such as a subroutine:
foo(("text" . "\n") x 3);

sub foo {
    # @_ is now the list ("text\n", "text\n", "text\n");
    my ($string) = @_;   # error enters here
    # $string is now "text\n"
}

This is a subtle difference which might not always give the desired result.
A better solution for this particular case is to not use the concatenation operator at all, because it is redundant:
print "$foo\n" x $count;

Or even use more mundane methods:
for (0 .. $count) {
    print "$foo\n";
}

Or
use feature 'say'
...
say $foo for 0 .. $count;


Q:

AttributeError: 'google.protobuf.pyext._message.RepeatedCompositeCo' object has no attribute 'append'

I am building a transfer learning model on the MobileNetv2 pretrained model on Google Collab. Till yesterday, everything was fine. But, today, on executing
#Create the base model(feature_extractor) from the pre-trained model MobileNet V2
_URL = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/2"
feature_extractor = hub.KerasLayer(_URL, input_shape=(_TARGET_SIZE, _TARGET_SIZE,3))

I get the error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-29-663d4cbb70df> in <module>()
      2 _TARGET_SIZE = 224
      3 _URL = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/2"
----> 4 feature_extractor = hub.KerasLayer(_URL, input_shape=(_TARGET_SIZE, _TARGET_SIZE,3))
      5 #print(feature_extractor._layers)

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/resource_variable_ops.py in _variable_handle_from_shape_and_dtype(shape, dtype, shared_name, name, graph_mode, initial_value)
    165     handle_data = cpp_shape_inference_pb2.CppShapeInferenceResult.HandleData()
    166     handle_data.is_set = True
--> 167     handle_data.shape_and_type.append(
    168         cpp_shape_inference_pb2.CppShapeInferenceResult.HandleShapeAndType(
    169             shape=shape.as_proto(), dtype=dtype.as_datatype_enum))

AttributeError: 'google.protobuf.pyext._message.RepeatedCompositeCo' object has no attribute 'append'

Any idea why this happens and do I need to get into the /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/resource_variable_ops.py file and make some changes ? I think its related to some update issue. Any help on how to tackle it?

A:

I had same error with tensorflow (version 2.2.0-dev20200128) and fixed it by upgrading protobuf (As explained in this issue):
pip install -U protobuf==3.8.0

Or if your using Notebook (like Google Colab notebook) try this:
!pip install -U protobuf==3.8.0

A:

If it was running fine until yesterday and you did not change anything, then check for two things-
Th TF version, if it has changed bc recently they set 'default' TF version to 1.15.rc from 1.14.
If that is same then save this file and close all the colab windows even your chrome or whatever browser you are using then open again and try running the file.
EDIT:
As I said above it must be because of the TF version. So revert back to the one that you used when the model was working. As you mentioned in your comments below it was working on version ‘dev20191010’, so rolling back to it will fix your issue.


Q:

Watir & Nokogiri not loading content in frame

Using watir & nokogiri to parse the content I can find in my bank account. The line browser.div(:id => 'main_layout_v2_1_cell_1:0').wait_until_present tells watir to wait until the div that is loaded by js appears. (I disabled javascript in Chrome to check if the content is loaded by javascript, and indeed it is.)
Nonetheless when Nokogiri puts browser.html it shows all the content except the section loaded by js.
require 'rubygems'
require 'watir'
require 'watir-webdriver'
require "watir-webdriver/wait"
require 'nokogiri'

browser = Watir::Browser.new
browser.goto 'https://particulares.gruposantander.es/SUPFPA_ENS/BtoChannelDriver.ssobto?dse_operationName=NavLoginSupernet&dse_parentContextName=&dse_processorState=initial&dse_nextEventName=start'

#Login
browser.select_list(:name => 'tipoDocumento').select 'NIF'
browser.text_field(:name => 'numeroDocumento').set 'xxx'
browser.text_field(:name => 'password').set 'xxx'
browser.button(:value => 'Entrar').click

#Select account
browser.link(:title => 'Cuentas').when_present.click
browser.div(:id => 'main_layout_v2_1_cell_1:0').wait_until_present

#Parse what you see, Noko
page = Nokogiri::HTML.parse(browser.html)
puts page

Things I've tried:

If I'm parsing the whole HTML through Nokogiri is because first I have tried to get those links I want to click finding by ID, title, text. None of them worked because as Nokogiri shows in the output, that part of code is not present.
Extending the timeout and rescueing the error to give the browser more time to make sure the code is there.

Code here:
    begin
Timeout::timeout(40) do
    #Parse what you see, Noko
    page = Nokogiri::HTML.parse(browser.html)
    puts page
  end
    puts 'done'
rescue Timeout::Error => e
  puts 'not done :/'
end

Wait_until a div present in the content loaded by js is present > browser.wait_until{browser.div(:id => 'main_layout_v2_1_cell_1:0').exist?} > Timeout error.

Notes: the content I'm trying to get is wrapped in a body tag with this structure <body scroll="auto" bgcolor="F4F6F7" onload="main.onload();">
The code parsed by Nokogiri only outputs the content that is not loaded by js. How to load that content too?

A:

The html method only does not include the contents of frames and iframes. As a result, if the desired content is within a frame, you need to explicitly tell Watir to return the frame HTML.
Assuming there is only 1 iframe on the page, you would do:
page = Nokogiri::HTML.parse(browser.iframe.html)


Q:

bold line not covered in test class how to covered?

public class contactTriggerHandler {
    Set<Id> AccountIds = new Set<Id>();
    public void performOnAfterInsert(List<Contact> lstCon){

        for (contact con : lstCon ) {

            if(con.AccountId != null){
                AccountIds.add(con.AccountId);

                List<Account> AcctToUpdate = new List<Account>();
                List<AggregateResult> cont=[Select Count(Id) ContactCount, AccountId from Contact where AccountId IN: AccountIds GROUP BY AccountId];

                for (AggregateResult ar: cont){
                        Account acc = new Account(Id=(Id)ar.get('AccountId'), My_Contact__c=(Integer)ar.get('ContactCount'));
                        AcctToUpdate.add(acc);
                }
                update AcctToUpdate;
                system.debug('insertworking');
            }
         }
    }

    public void performOnAfterUpdate(List<contact> lstcon, Map<Id, Contact> oldMapCon) {
    system.debug('!!enter');
        for (contact con : lstcon) {
                  if(con.AccountId!=null && oldMapCon.get(con.Id).AccountId != null){
                        AccountIds.add(oldMapCon.get(con.Id).AccountId);
                        system.debug('@1-'+oldMapCon.get(con.Id).AccountId);
                  }

                  if(con.AccountId!=null && oldMapCon.get(con.Id).AccountId != null && con.AccountId != oldMapCon.get(con.Id).AccountId){
                       **AccountIds.add(con.AccountId);**
                       system.debug('@2-'+con.AccountId);
                        **AccountIds.add(oldMapCon.get(con.Id).AccountId);**
                        system.debug('@3-'+oldMapCon.get(con.Id).AccountId);
                   }

                  if(con.AccountId!=null && oldMapCon.get(con.Id).AccountId == null){
                        **AccountIds.add(con.AccountId);**
                        system.debug('@4-'+con.AccountId);
                  }

        List<Account> AcctToUpdate = new List<Account>();
        List<AggregateResult> cont=[Select Count(Id) ContactCount, AccountId from Contact where AccountId IN: AccountIds GROUP BY AccountId];

        for(AggregateResult ar: cont){
            Account acc = new Account(Id=(Id)ar.get('AccountId'), My_Contact__c=(Integer)ar.get('ContactCount'));
            AcctToUpdate.add(acc);
        }
            update AcctToUpdate;
     }
    }
}

A:

You simply need to do two things: (a) change the account ID from one account to another, and (b) change the account ID from a valid account to nothing. Assuming you have a variable for your contact, you can do this:
someContact.AccountId = someNewAccount.Id;
update someContact;
// Validate behavior, then...
someContact.AccountId = null;
update someContact;
// Validate behavior


Q:

ASP.NET MVC3: Object from DropdownList in Null in Controller

I have created a sample MVC3 application for BookShop. I have a create action. In the create action users enter name of a book, author name and select a genre name. It is working without errors. But in the controller, I am not getting the selected genre name ( - the genre object comes as null). How do we correct it?
Note: When I write @Html.DropDownList("GenreId", String.Empty) the Model Binding capabilities built into ASP.NET MVC, will attempt to populate an object of that type using form inputs. E.g. it will try to set the book object’s GenreId value.  But Book does not contain the property GenreId. However the dropdown can show the values by reading required values from ViewBag.
CODE
@model MyBookShopApp.Book

@{
ViewBag.Title = "Create";
}

Create

@using (Html.BeginForm()) {

@Html.ValidationSummary(true)

<fieldset>

    <legend>BOOK</legend>

        @Html.LabelFor(model => model.Title) :~: @Html.EditorFor(model => model.Title)

        @Html.LabelFor(model => model.Artist.Name )  :*: @Html.EditorFor(model => model.Artist.Name)

        @Html.LabelFor(model => model.GenreEntity.Name, "The Genre") ::   @Html.DropDownList("GenreId", String.Empty)

        <input type="submit" value="Create" />

</fieldset>
}

 @Html.ActionLink("Back to List", "Index")

CONTROLLER
namespace MyBookShopApp.Controllers
{
public class StoreManagerController : Controller
{

    List<GenreEntity> listOfGenres = new List<GenreEntity>()
                                    {
                                       new GenreEntity { Name = "Fiction", GenreId = 1 },
                                       new GenreEntity { Name = "Science", GenreId = 2 },
                                       new GenreEntity { Name = "Religious", GenreId = 3 },
                                    };

    List<Book> bookList = new List<Book>()
                          {
                            new Book
                            {
                                BookId = 1,Title = "Book1",
                                GenreEntity = new GenreEntity { Name = "Fiction", GenreId = 1 },
                                Artist = new Artist { ArtistId = 1, Name = "Dinesh" }
                            },
                            new Book
                            {
                                BookId = 2,Title = "Book2",
                                GenreEntity = new GenreEntity { Name = "Science", GenreId = 2 },
                                Artist = new Artist { ArtistId = 1, Name = "Lijo" }
                            },
                            new Book
                            {
                                BookId = 3,Title = "Book3",
                                GenreEntity = new GenreEntity { Name = "Religious", GenreId = 3 },
                                Artist = new Artist { ArtistId = 1, Name = "Santhosh" }
                            }

                          };

    #region CREATE

    // GET:
    public ActionResult Create()
    {
                    ViewBag.GenreId = new SelectList(listOfGenres, "GenreId", "Name");
        return View();
    }

    // POST:
    [HttpPost]
    public ActionResult Create(Book theBook)
    {
        if (ModelState.IsValid)
        {
            //Save the book in DB first and then redirectToAction.
            return RedirectToAction("Index");
        }

        return View(theBook);
    }

    #endregion

}
}

Book Class
public class Book
{
    public int BookId { get; set; }
    public string Title { get; set; }
    public GenreEntity GenreEntity { get; set; }
    public Artist Artist { get; set; }

}

GenreEntity
  public class GenreEntity
{
    public string Name { get; set; }
    public int GenreId { get; set; }
}

Artist Class
 public class Artist
{
    public int ArtistId { get; set; }
    public string Name { get; set; }
}

REFERENCE:

dropdown in mvc3 edit form
How to use a Html.DropDownList in a strongly-typed view for a model containing a nullable enum property?
MVC3 HTML helper doesn't update DropdownListFor on Submit the form
on select change event - Html.DropDownListFor
MVC3 @Html.DropDownListFor not populating selected item

A:

@Html.DropDownList("GenreId", String.Empty) creates a SELECT with the name GenreId. However MVC3 is expecting the name GenreEntity.Name to bind to Book.
The simple solution is to modify public ActionResult Create(Book theBook)
  to public ActionResult Create(Book theBook, string genreId) to receive the genreId that got posted back
Alternatively
@Html.LabelFor(model => model.GenreEntity.GenreId, "The Genre")
@Html.DropDownListFor(model => model.GenreEntity.GenreId, (SelectList)ViewBag.GenreId, String.Empty)

Remember to set your ViewBag inside the HttpPost Create method also.
Edit
Corrected the property name from BookId to GenreId


Q:

R packages or open source software for training Hidden Markov chains

Are there any well-designed R packages or other open-source software for training Hidden Markov chains?

A:

There are several. For example, (with R) you could use the 'HiddenMarkov' package to train and find parameters for HMM models using common algorithms (e.g. Baum-Welch).
There was also a related thread on stack overflow, which was apparently closed:


Q:

How to send an email through gmail without enabling 'insecure access'?

Google are pushing us to improve the security of script access to their gmail smtp servers.  I have no problem with that.  In fact I'm happy to help.
But they're not making it easy.  It's all well and good to suggest we Upgrade to a more secure app that uses the most up to date security measures, but that doesn't help me work out how to upgrade bits of code that look like this:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(GMAIL_USER, GMAIL_PASSWORD)
server.sendmail(FROM, TO, MESSAGE)
server.close()

Sure, I'll go and turn on "Access for less secure apps", but if anyone has worked out what to replace this code with, I'll be grateful.

A:

This was painful, but I seem to have something going now...
Python3 is not supported (yet)
I don't think it will be too hard to attain, as I was stumbling through converting packages without hitting anything massive: just the usual 2to3 stuff.  Yet after a couple of hours I got tired of swimming upstream.  At time of writing, I couldn't find a published package for public consumption for Python 3.  The python 2 experience was straight-forward (in comparison).
Navigating the Google website is half the battle
No doubt, over time, this will change.  Ultimately you need to download a client_secret.json file.  You can only (probably) do this setting up stuff via a web browser:

You need a google account - either google apps or gmail. So, if you haven't got one, go get one.
Get yourself to the developers console
Create a new project, and wait 4 or 400 seconds for that to complete.
Navigate to API's and Auth -> Credentials
Under OAuth select Create New Client ID
Choose Installed Application as the application type and Other
You should now have a button Download JSON. Do that.  It's your client_secret.json—the passwords so to speak

But wait that's not all!
You have to give your application a "Product Name" to avoid some odd errors. (see how much I suffered to give you this ;-)

Navigate to API's & auth -> Consent Screen
Choose your email
Enter a PRODUCT NAME.  It doesn't matter what it is. "Foobar" will do fine.
Save

Newsflash! Whoa. Now there's even more!

Navigate to API's & auth -> APIs -> Gmail API
Click the button Enable API

Yay. Now we can update the emailing script.
Python 2
You need to run the script interactively the first time.  It will open a web browser on your machine and you'll grant permissions (hit a button).  This exercise will save a file to your computer gmail.storage which contains a reusable token.
[I had no luck transferring the token to a machine which has no graphical browser functionality—returns an HTTPError.  I tried to get through it via the lynx graphical browser. That also failed because google have set the final "accept" button to "disabled"!? I'll raise another question to jump this hurdle (more grumbling)]
First you need some libraries:
pip install --upgrade google-api-python-client
pip install --upgrade python-gflags

you need to change the to and from addresses
make sure you have the client_token.json file whereever the Storage instructions expect  it
the directory needs to be writable so it can save the gmail.storage file

Finally some code:
import base64
import httplib2

from email.mime.text import MIMEText

from apiclient.discovery import build
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run

# Path to the client_secret.json file downloaded from the Developer Console
CLIENT_SECRET_FILE = 'client_secret.json'

# Check https://developers.google.com/gmail/api/auth/scopes for all available scopes
OAUTH_SCOPE = 'https://www.googleapis.com/auth/gmail.compose'

# Location of the credentials storage file
STORAGE = Storage('gmail.storage')

# Start the OAuth flow to retrieve credentials
flow = flow_from_clientsecrets(CLIENT_SECRET_FILE, scope=OAUTH_SCOPE)
http = httplib2.Http()

# Try to retrieve credentials from storage or run the flow to generate them
credentials = STORAGE.get()
if credentials is None or credentials.invalid:
  credentials = run(flow, STORAGE, http=http)

# Authorize the httplib2.Http object with our credentials
http = credentials.authorize(http)

# Build the Gmail service from discovery
gmail_service = build('gmail', 'v1', http=http)

# create a message to send
message = MIMEText("Message goes here.")
message['to'] = "yourvictim@goes.here"
message['from'] = "you@go.here"
message['subject'] = "your subject goes here"
body = {'raw': base64.b64encode(message.as_string())}

# send it
try:
  message = (gmail_service.users().messages().send(userId="me", body=body).execute())
  print('Message Id: %s' % message['id'])
  print(message)
except Exception as error:
  print('An error occurred: %s' % error)

Hopefully that gets us all started.  Not as simple as the old way, but does look a lot less complicated now I can see it in the flesh.

A:

It seems that John Mee's answer is out of date.
It does not work in July, 2016.
Maybe due to the update of Gmail's API.
I update his code (python 2) as below:
    """Send an email message from the user's account.
"""

import base64
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import mimetypes
import os

#from __future__ import print_function
import httplib2
import os

from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools

from apiclient import errors

SCOPES = 'https://www.googleapis.com/auth/gmail.compose'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Gmail API Python Quickstart'

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

def SendMessage(service, user_id, message):
  """Send an email message.

  Args:
    service: Authorized Gmail API service instance.
    user_id: User's email address. The special value "me"
    can be used to indicate the authenticated user.
    message: Message to be sent.

  Returns:
    Sent Message.
  """
  try:
    message = (service.users().messages().send(userId=user_id, body=message)
               .execute())
    print 'Message Id: %s' % message['id']
    return message
  except errors.HttpError, error:
    print 'An error occurred: %s' % error

def CreateMessage(sender, to, subject, message_text):
  """Create a message for an email.

  Args:
    sender: Email address of the sender.
    to: Email address of the receiver.
    subject: The subject of the email message.
    message_text: The text of the email message.

  Returns:
    An object containing a base64url encoded email object.
  """
  message = MIMEText(message_text)
  message['to'] = to
  message['from'] = sender
  message['subject'] = subject
  return {'raw': base64.urlsafe_b64encode(message.as_string())}

def get_credentials():
    """Gets valid user credentials from storage.

    If nothing has been stored, or if the stored credentials are invalid,
    the OAuth2 flow is completed to obtain the new credentials.

    Returns:
        Credentials, the obtained credential.
    """
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                                   'sendEmail.json')

    store = oauth2client.file.Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else: # Needed only for compatibility with Python 2.6
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials

if __name__ == "__main__":

    try:
        credentials = get_credentials()
        http = credentials.authorize(httplib2.Http())
        service = discovery.build('gmail', 'v1', http=http)
        SendMessage(service, "me", CreateMessage("send@gmail.com", "receive@gmail.com", "Test gmail automation", "Hello world"))

    except Exception, e:
        print e
        raise

Note that if you encounter the error Insufficient Permission, one possible reason is that the scope in program is not set correctly.
The other possible reason may be that you need to delete the storage json file ("sendEmail.json" in this program) and refresh your program. More details may be seen in this post.

A:

Have you considered using the Gmail API? The API has security features built in and is optimized specifically for Gmail. You can find the API documentation on http://developers.google.com - for example, here's the documentation for the Send API call:
https://developers.google.com/gmail/api/v1/reference/users/messages/send


Q:

Magento2 TypeError: cart is not a function

I ran into a issue where i could add products to cart and i got success message too but the minicart is not updating with the added items.
I got below error in console, I have upgraded the magento to 2.1.0

TypeError: cart is not a function in knockout.js

Did anyone came across this issue.

A:

I have updated the bind events in the minicart file by comparing the base codes, once after update. It worked.
Just compare the minicart.phtml file from theme

app\design\frontend\Theme_namespace\Theme_module\Magento_Checkout\templates\cart

with core template

vendor\magento\module-checkout\view\frontend\templates\cart

In these two files class and bind events may be different in your theme, that is why it is not updating.


Q:

Base constructor not being called/resolved properly

Background info:
I'm using Microsoft Visual C# 2010 for this project. I want to use a class, System.IO.DirectoryInfo, to get a directory listing. However, the implementation of GetDirectories() doesn't work for me, since upon throwing an UnauthorizedAccessException exception, the function fails.
I would like to create a DLL that defines a derived class having the same functionality of the DirectoryInfo class, but overrides GetDirectories() so I can have the functionality I need.
The problem:
I must be doing something wrong when trying to call the base constructor. I get the error "'object' does not contain a constructor that takes 1 arguments".
using System;
using System.IO;

namespace CompanyName.System.IO
{
    public class FlexibleDirectoryInfo : DirectoryInfo
    {
        public FlexibleDirectoryInfo(string path) : base(path) {}
        [...]

DirectoryInfo does not have a default constructor, and instead has a single constructor taking a string parameter, just like I'm trying to do. However, I'm not sure if it's resolving the base constructor properly?
I have a feeling this is a simple fix. Please just help point out what I'm overlooking. :)
Thanks!

A:

DirectoryInfo is a sealed class. So you cannot derive from it.
From MSDN:

public sealed class DirectoryInfo : FileSystemInfo

However, you create a class that inherits FileSystemInfo (superclass for DirectoryInfo). You'll have to create most of the implementation, but it's the only way.


Q:

Comparing nodes of a list with a string array

If i have an array that contains some char such as [a,b,c] and i have another array that contains the respective frequency of each char such as [2,1,1]. I would like to now go through a linked list which has nodes which have some string to see if they also have the chars i have in my original array with same frequency.
My approach
I was thinking i need
One loop that will start at index 0 of original array and another loop inside that will check all nodes for that string and if my temp pointer hits null it means all of them have it and if not then they don't and i move on to the next one. However i am not sure how to quite implement this approach as i am very new to c and also i was wondering is it possible to do this in O(N) TIME as my approach would be O(N2).
Sample Output: i apologize for the confusion
so if you have 3 nodes and each has a char array containing "nba" "tba" "rba"
the output should then return b a  . since both them appear equal number of times in each node.

A:

So you start both your char array and freqarray at index 0 and then check all the nodes for strings matching the same frequency of a character . I presume you use some kind of function to return frequency of particular char in a string .
Also your problem requires you to go through all of the nodes hence O(N^2) is implied.


Q:

Javascript Packing with many constraints, looking for a simple solution

This is not exactly packing as I can assign rectangle sizes myself, I just need a packed result.
I have rectangles of same ratio and different sizes
FULL,
HALF(area = 1/4 * FULL),
QUARTER(area = 1/4 * HALF).
All will be positioned horizontally only. The container is of width 3 times FULL and height will adjust to fit rectangles.
There will be 150 rectangles which will be given random sizes from array(full,half,quarter). Now I want to arrange these rectangles in the container so that there is no gap.
The container and rectangles are HTML DIVs. I am using JavaScript to pack them.
Here is a fiddle http://jsfiddle.net/MywQ2/1/
In the above code I tried to constrain the selection of next box depending on present one.
May be i am not clear, i will try to explain again. I have 150 boxes, i just want to fill container with 150 boxes, they should be randomly of size full,half,quarter. We can also reject randomly selected size and get another one if it is found to create gap.

A:

I think you either want something impossible or you formulate it wrong. If you pick your rectangles truly random, you will end up with gaps in your container.
In the sample code you have now you pick them semi-random, as it depends on the modulus of status.
If your goals is to fill the container with seemingly random rectangles, maybe next algorithm works for you (peude code):
for rectangle in ['large', 'medium', 'small']:
    try:
        place_rectangle_randomly_in_container(rectangle)
    except NoFreeSpace:
        if rectangle == 'small':
            # container filled
            break

where the place_rectangle_randomly_in_container tries to put the rectangle anywhere in the container at random.
To implement the placement, keep track of the container with a 2-dimensional array of booleans which indicate if that spot is still free; every element in the array represents the space a small rectangle would fill, so if the container can contain 12x12 small rectangles, that would be the dimension of the array. To check if a medium rectangle would fit at [2,3], you need to check the array for [2,3], [2,4], [3,3] and [3,4].
Placing the rectangles is then done by positioning them instead of floating them left.


Q:

Julia/Dates : convert a timespan from "days" to "year" or "Float64"

I would like to convert a timespan from "days" to "year" or "Float64"
let be at timespan t:
t = Date("2000-02-04") - Date("1996-06-04")

However each following lines give me an error
t/365
Float64(t)
parse(Float64,t)
convert(Dates.Year, t)

A:

You can get the value stored in t and divide it by 365
julia> Dates.value(t)/365
3.671232876712329

Note however that this assumes that each year is 365 days which is not true. For some scenarios a more elegant solution would be to count the years assuming that the date starts at some point, have a look at the example below:
julia> d0 = Date("2000-01-01")
2000-01-01

julia> d1 = d0 + t
2003-09-02

julia> year(d1)-year(d0), month(d1)-month(d0), day(d1) - day(d0)
(3, 8, 1)


Q:

Joining 3 three tables

I have this diagram which should explain my situation I need some help on joining 3 tables which I have no idea how to do this kind of thing:

So I can go through a while loop of retrieving the records by doing this:
<img src="<?php echo $row['filename']; ?>" alt="" />

Album: <?php echo $row['album_name']; ?>
AlbumID: <?php echo $row['album_id']; ?>

A:

Using an INNER JOIN will prevent returning albums that don't have images. The ORDER BY ... DESC will sort the results in descending order but I'm not sure how to only return the last record. It would take some sort of combination of ORDER BY, GROUP BY and TOP, perhaps.
SELECT
    album_table.album_id,
    album_table.album_name,
    images_table.filename
FROM album_table
INNER JOIN images_table ON images_table.album_id = album_table.album_id
WHERE album_table.user_id = uid
ORDER BY images_table.date DESC


Q:

How do I set a cell in a DataSet? HELP!

I´m trying to set a cell of type "System.Int32" in a DataSet by code, and my try looks like this:
int aCarID = 5; // as an example...

// points out the row that I want to manipulate - I guess this is what doesn´t work???
int insertIndex = myDataSet.tableCars.Rows.Count

myDataSet.tableCars.Rows[insertIndex]["CarID"] = aCarID;

What happens is: I get an exception of "System.IndexOutOfRangeException".
You´re allowed to say that I´m stupid as long as you provide an answer...
UPDATE!
Yes, I´m trying to create a new row, that´s true - that´s why I´m not using "-1".
So what´s the syntax to create a new row?
If I use tableCars.Rows.Add(...) I need to supply a "DataRow Row" to the Add-function, and I don´t have one to provide - yet! (Catch 22)
NEW UPDATE!
Ooops, found it - "NewRow()" :-)

A:

You do realize that indices start with zero in C#? That means if your table has 3 rows, you're trying to access the 4th row because insertIndex = 3.
Try insertIndex - 1.
Edit: Since you're trying to add a new row and already found out how to do so, also don't forget to save those changes to the database (I assume that's what you want to do). The most simple way is to set the UpdateCommand-property of the DataAdapter you used to fill the DataSet (or actually the DataTable in the DataSet).
You can also have the update commands generated, using a subclass of the DbCommandBuilder.


Q:

Limit socket concurrent connection

My server socket can support maximum possible connections. But how can I limit the number of concurrent connections , in such a way that it can support at least 5 but not more than 20 clients concurrently.

A:

You have two choices:

Simply do not call accept() more times then you are prepared to handle.  If you have 20 clients already connected, stop calling accept() again until a client disconnects first.

keep calling accept() normally, but immediately call close() (or closesocket() on Windows) if you already have 20 clients connected.
a. A variation on this on Windows only is to use WSAAccept() instead. It lets you specify a callback to conditionally accept clients.  You can have it return CF_REJECT while 20 clients are already connected.  That way, WSAAccept() will never allow more than 20 clients to be connected at the same time.


Q:

Working with system-level dictionaries

I've been working on a Qt-based C++ program called RoboJournal. It uses Hunspell for spell-checking purposes. Right now, it automatically installs its own dictionary files (en_US.dic) into the user's home folder (i.e. ~/.robojournal/en_US.dic). This works fine  as-is on OSes like Windows that provide no compatible dictionaries out of the box but there is a design problem I want to fix:
On Linux, there are several instances of en_US.dic (most notably in /usr/share/hunspell/en_US.dic) This copy is obviously meant to be shared between all programs that use Hunspell, which means that my program should use it if possible instead of relying on its own copy. However, the copy in /usr/share/hunspell/ requires superuser rights to modify; while this means that users may read from it they can't  add words to it unless the program is running with superuser permissions or unless I chmod the file to allow everyone to write to it. This problem makes the /usr/share/hunspell/en_US.dic copy much less useful.
Is there a way for users to modify the /usr/share/hunspell/en_US.dic file without having superuser permissions or requiring them to chmod the file first? My Debian package maintainer said that my app should use global system resources whenever possible.

A:

In situations like this, usually you load the system defaults, and then you load the user specific settings file.
So when your program has been running and the user ignores some words, and adds others, etc. you save this to the user's dictionary in their application data directory.
If you really want your program to save additions to the system wide changes to the default dictionary, you should prompt the user, if they want to save dictionary changes to the system defaults, and then open a superuser login, or whatever credentials that you would need to save edits to that file, and then save it.
Or you could even have it where if the program was started initially as a superuser, then it saves it to the system folder, otherwise it saves it to the user's application directory.
The model that I follow for settings and what not, is explained in QSettings.
http://qt-project.org/doc/qt-4.8/qsettings.html#details
http://qt-project.org/doc/qt-4.8/qsettings.html#platform-specific-notes
QSettings handles everything rather elegantly if you set up your application name and organization name.  It doesn't do hunspell dictionaries out of the box, but the locations/paths it uses are significant.
Hope that helps.


Q:

C# aggregate in a better time complexity

Let's say we have such an array:
var arr = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

I want to perform the aggregation:
var sum = arr.Aggregate((a, b) => a + b);

Of course, this is just an example to simplify it. I don't deal with int, but more complex objects that need merging (they are trees). However, This aggregation works really bad, because it iterates from left to right, adding two elements that lie to each other. In case of ints this doesn't make any difference, but in case of complex objects a better solution would be to perform the aggregation in a tree way. What does it mean?
                   55
              36         19
      10             26      19
  3       7      11      15      19
1   2   3   4   5   6   7   8   9   10

I hope this schema makes it clear.
How to achieve this in C#'s LINQ?

A:

You can use PLINQ (parallel LINQ).
The Parallel Aggregation pattern uses unshared, local variables that are merged at the end of the computation to give the final result. Using unshared, local variables for partial, locally calculated results is how the steps of a loop can become independent of each other.
If you invoke the AsParallel extension method, you're instructing the compiler to bind to PLINQ instead of to LINQ. The program will use the parallel versions of all further query operations within the expression.
In your case the code would be:
var sum = arr.AsParallel().Aggregate((a, b) => a + b);

Here you can find more information: https://msdn.microsoft.com/en-us/library/ff963547.aspx

A:

You can create a LINQ-like extension method that aggregates a sequence "top-down" in a hierarchical fashion like you describe. However, for efficiency this requires random access to the source sequence so building on top on IEnumerable<T> is not the best choice. But you can use IReadOnlyList<T> as an alternative (which off course requires that your source is stored in an array or list).
static class ReadOnlyListExtensions {

  public static T HierarchicalAggregate<T>(this IReadOnlyList<T> source, Func<T, T, T> func) {
    if (source == null)
      throw new ArgumentNullException("source");
    if (func == null)
      throw new ArgumentNullException("func");
    if (source.Count == 0)
      throw new InvalidOperationException("Sequence contains no elements");
    return Recurse(source, 0, source.Count, func);
  }

  static T Recurse<T>(this IReadOnlyList<T> source, Int32 startIndex, Int32 count, Func<T, T, T> func) {
    if (count == 1)
      return source[startIndex];
    var leftCount = count/2;
    var leftAggregate = Recurse(source, startIndex, leftCount, func);
    var rightCount = count - leftCount;
    var rightAggregate = Recurse(source, startIndex + leftCount, rightCount, func);
    return func(leftAggregate, rightAggregate);
  }

}

Note that the division performed by this algorithm is slightly different compared to your example. At the first level the 10 element sequence is divided into two 5 element sequences that are then each is divided into a 2 and a 3 element sequence etc.:

        55
   15   +   40
 3 + 12    13+27
1+2 3+9   6+7 8+19
     4+5       9+10


Q:

Testing FastAPI FormData Upload

I'm trying to test the upload of a file and its metadata using Python and FastAPI.
Here is how I defined the route for the upload:
@app.post("/upload_files")
async def creste_upload_files(uploaded_files: List[UploadFile], selectedModel: str = Form(...),
                              patientId: str = Form(...), patientSex: str = Form(...),
                              actualMedication: str = Form(...), imageDim: str = Form(...),
                              imageFormat: str = Form(...), dateOfScan: str = Form(...)):
    for uploaded_dicom in uploaded_files:
        upload_folder = "webapp/src/data/"
        file_object = uploaded_dicom.file
        #create empty file to copy the file_object to
        upload_folder = open(os.path.join(upload_folder, uploaded_dicom.filename), 'wb+')
        shutil.copyfileobj(file_object, upload_folder)
        upload_folder.close()
    return "hello"

(I'm not using the metadata but I will later).
I use unittest for the testing:
class TestServer(unittest.TestCase):
    def setUp(self):
        self.client = TestClient(app)
        self.metadata = {
            "patientId": "1",
            "patient_age": "M",
            "patientSex": "59",
            "patient_description": "test",
            "actualeMedication": "test",
            "dateOfScan": datetime.strftime(datetime.now(), "%d/%m/%Y"),
            "selectedModel": "unet",
            "imageDim": "h",
            "imageFormat": "h"
        }

    def tearDown(self):
        pass

    def test_dcm_upload(self):
        dicom_file = pydicom.read_file("tests/data/1-001.dcm")
        bytes_data = dicom_file.PixelData

        files = {"uploaded_files": ("dicom_file", bytes_data, "multipart/form-data")}
        response = self.client.post(
            "/upload_files",
            json=self.metadata,
            files=files
        )
        print(response.json())

But it seems that the upload doesn't work, I get the following print of the response:
{'detail': [{'loc': ['body', 'selectedModel'], 'msg': 'field required', 'type': 'value_error.missing'}, {'loc': ['body', 'patientId'], 'msg': 'field required', 'type': 'value_error.missing'}, {'loc': ['body', 'patientSex'], 'msg': 'field required', 'type': 'value_error.missing'}, {'loc': ['body', 'actualMedication'], 'msg': 'field required', 'type': 'value_error.missing'}, {'loc': ['body', 'imageDim'], 'msg': 'field required', 'type': 'value_error.missing'}, {'loc': ['body', 'imageFormat'], 'msg': 'field required', 'type': 'value_error.missing'}, {'loc': ['body', 'dateOfScan'], 'msg': 'field required', 'type': 'value_error.missing'}]}

It is possible that I should upload using a Formdata instead of the body request (json=self.metadata) but I don't know how it should be done.

A:

The answer is just to replace json=self.metadata which can be used for body parameters by data=self.metadata for a formData


Q:

Not sure if my Inner join is efficient SQL

SELECT concat(e.FirstName, e.LastName), o.OrderID, o.OrderDate, p.ProductName, c.CategoryName, od.Quantity, od.UnitPrice, od.Discount, ((od.UnitPrice* od.Quantity)*(1-od.Discount)) AS 'Total Item Price', Country, Region, City
FROM Employees e
INNER JOIN Orders o
    ON  e.EmployeeID = o.EmployeeID
INNER JOIN [Order Details] od
    ON o.OrderID = od.OrderID
INNER JOIN Products p
    ON od.ProductID = p.ProductID
INNER JOIN Categories c
    ON p.CategoryID = c.CategoryID
WHERE e.Country = 'USA' OR e.Country = 'France' OR e.Country = 'Germany' OR e.Country = 'Brazil'
ORDER BY OrderDate DESC, e.FirstName;

I'm thinking there is a much easier way to do this query? Any input on this guys?

A:

It looks like the inner joins are doing what you need.
The only thing I would change is the where, I would change it to
WHERE e.Country IN ('USA', 'France', 'Germany', 'Brazil')

Thank you


Q:

Unable to store Model in Commerce Order Fields

I'm trying to use custom order fields in Craft Commerce to store a reference to an object, but all I am getting back is an ElementCriteria model.
I have two custom order fields defined: twoDayOption and fourDayOption. Both are events from Solspace Calendar (Calendar_EventElementType).
In the Commerce -> Orders page, I can set these fields with no problem.
On my product page I have a dropdown for selecting the values of each of these fields. They look pretty much the same. Here is the twoDayOption one:
<select name="fields[twoDayOption]">
    {%- for days in craft.calendar.events({calendar: 'workshops'}) -%}
        <option value="{{ days.id }}">{{ days.title }}</option>
    {%- endfor -%}
</select>

After submitting the form the values for twoDayOption and fourDayOption seem to be lost. When I call {{ dump( cart.twoDayOption) }} I get an object of type Craft\ElementCriteriaModel. It references the Calendar_EventElementType, but the ID that was submitted is not there.
Here's the first part of the output of that dump:
object(Craft\ElementCriteriaModel)#757 (17) {
  ["strictAttributes":protected]=>
  bool(false)
  ["_elementType":"Craft\ElementCriteriaModel":private]=>
  object(Craft\Calendar_EventElementType)#990 (7) {
    ["componentType":protected]=>
    string(11) "ElementType"
    ["_sourcesByContext":"Craft\BaseElementType":private]=>
    NULL
    ["_classHandle":"Craft\BaseComponentType":private]=>
    NULL
[snip]

Looking through Commerce_CraftController, I found that it calls BaseElementModel->setContentFromPost("fields") to set the custom order fields. I added in some logging statements to make sure it was getting the information I expected.
When it enters the method it finds these values from $_POST['fields']:
    Array
    (
        [twoDayOption] => 16
        [fourDayOption] => 12
    )
This is exactly whe I expect. It then finds the two custom field handles that match this. I have other logging statements that show what handle it's processing, and the value it gets from the POST:
2016/09/29 13:54:39 [info] [application] setContentFromPost twoDayOption
2016/09/29 13:54:39 [info] [application] setContentFromPost twoDayOption: 16

setContentFromPost also gets the fieldType, and calls $fieldType->prepValueFromPost. The value 16 is passed in, and 16 is returned.
Finally, that value is put into $this->_content->$handle, and I have a logging statement immediately after that verifies I can retrieve 16 from it. But when I log $this->twoDayOption, I'm getting the ElementCriteriaModel again.
I'm at a loss. It seems to know it's supposed to have a Calendar Event for that field. Should I not be passing in the ID? Is there another step I am missing?

A:

When you assign element IDs to a relation field you need to pass the data in an array format.
See this related answer: In a plugin, how do I save an entry with a related entry
Key information in that answer:
<input type="text" name="fields[customTextField]" value="My Text Data">
<input type="text" name="fields[customRelationField][]" value="101">

Note that in the input field name of 'customRelationField' relations field we are passing an array: fields[customRelationField][].
Also note the relation field data is a full replace. So if you ever want to add an element ID to an existing relation field that has relation already set, you need to get the existing IDs as an array and add your additional IDs when updating the field.


Q:

Why am I getting if: Expression Syntax when I try to run this script?

I am currently trying to run a Unix Executable File in terminal (my shell is TCSH) I downloaded online and I keep getting the following error:
if: Expression Syntax
Here is the script I am trying to run:
if [ -f .1 ]
then
cc -o xrdcalc .source/xrdcalc-1.1.c -lm
chmod 700 xrdcalc
./xrdcalc
else
platform=`uname`
echo
echo
echo "You are using \"xrdcalc\" for the first time on $platform , read the \"Readme.txt\" file and then proceed"
echo
echo
echo "Press enter...."
read char;
echo `date` > .1
mkdir .source
mv xrdcalc-1.1.c .source
cc -o xrdcalc .source/xrdcalc-1.1.c -lm
./xrdcalc
fi

I have little experience with running scripts and I am sure it is an easy fix.

A:

There are other issues with this code that indicate it was coded for traditional sh or bash.
Just put #!/bin/bash as the first line (using the correct path to your system's copy of bash), and it should work without other modifications.
Of course,
 chmod 755 scriptName

is also required to "mark" the file as executable and if the file is saved to a directory not in the path, you need to either cd to the correct dir, or invoke as
/full/path/to/scriptName

If you're using a reduced version of Linux that doesn't have bash installed and you can't install it for some reason, then look for other 'Bourne Shell' derived shell processors, like ash, dash, ksh, sh .
IHTH


Q:

Confusing result obtained taking second derivative of ye^y

I was doing my calculus homework, and one of the questions asked for the first and second derivative of $ye^y=x$, I did the computations and arrived at $-(x+1)^{-2}$, which was a lot neater and simpler than I expected, I am particularly perplexed by the fact that this expression doesn't have a $y$ in it, while my first derivative $\bigg(\frac{y}{yx+y}\bigg)$ did.
Is my math correct, and if it is, is there any way this result could be explained non-mathematically, in such a way that I might be able to understand why this would be true?
My math:
$$ye^y=x \rightarrow ln(y) + y = ln(x) \rightarrow \frac{d}{dx}\big[ln(y)+y\big]=\frac{d}{dx}\big[ln(x)\big]\rightarrow\frac{dy}{dx}=\frac{y}{xy+y}$$
for the first derivative, and
$$\frac{d^2y}{dx^2}=\frac{y'(xy+y)-(xy+y)'y}{(xy+y)^2}=\frac{\frac{y}{xy+y}(xy+y)-y[y'(x+1)+(x+1)'y]}{y^2(x+1)^2}=\frac{y-y[\frac{y}{xy+y}(x+1)+y]}{y^2(x+1)^2}=\frac{y-y(1+y)}{y^2(x+1)^2}=\frac{y-y-y^2}{y^2(x+1)^2}=-\frac{1}{(x+1)^2}=-(x+1)^{-2}
$$
for the second dericative.

A:

In that step when you first took the derivative of $y$ you should get $\frac{dy}{dx}= \frac{y}{(y+1)x}$, right?


Q:

perl replace characters in a string but retain special character or space

I would like to create a program that replaces characters and retains the special characters. An example input and output is shown below.
Here's what I did so far:
$sentence = userinput;

@words = split(/ /, $sentence);

for ($i = 0; $i < @words.length; $i ++){
    $words[$i] =~ s/\W//g;
    @characters = split(//, $words[$i]);
    #print $words[$i] . "\n";

    $wordlength = length($words[$i]);

    for ($j = 0; $j < @characters.length; $j ++){

    $char = $characters[$j];

    for ($x = 0; $x < $wordlength; $x++){
        $char++;
        if ($char eq "aa"){
            $char = "a";
        }
        elsif ($char eq "AA"){
            $char = "A";
        }
    }
    print $char;
    if ($x = 0){
        $output[$i] = $char;
    }
    else {
        $output[$i] = join ($char);
    }
}

print $output[$i];
}

Input:

Hi! how are you doing?

Output:

Jk! krz duh brx itnsl?

A:

A couple of things in your code don't make sense:

Missing use strict; use warnings;.
All variables are global (you should be using my to create variables)
@foo.length is not the number of elements in the array @foo. It's the number of elements in the array @foo concatenated with the number of characters in $_ (because arrays in scalar context return their length, . concatenates strings, and length works on $_ by default).
join ($char) always returns the empty string: You're joining an empty list (no elements) using $char as a separator.

Here's an attempt to fix all of these issues:
use strict;
use warnings;

my $sentence = readline;

$sentence =~ s{([A-Za-z]+)}{
    my $word = $1;
    join '', map {
        my $base = ord(/^[A-Z]/ ? 'A' : 'a');
        chr((ord($_) - $base + length($word)) % 26 + $base)
    } split //, $word
}eg;

print $sentence;


Q:

Exacttarget Calendar notifications

is there any chance to receive notifications as soon as a Event on Calendar is deleted or changed?
Like it is in Sales Cloud where i can simply choose to send
Thank you.

A:

No. There's no way to do this in realtime by webhook, trigger or similar. Indeed, there's no way to get this information easily after the fact in an automated way, either.
This "Interactive Marketing Hub" functionality was the focus of development between 2011 and 2013 where there was a rapid move to developing as many features as possible to sell a cross-channel story for acquisition.
I hope there's a Product developer or two on the case filling in some of these gaps. There are more shiny things that make the difference in RFPs or impress the chain of command these days, though. I'm not holding my breath.


Q:

Unsupervised pre-training for convolutional neural network in theano

I would like to design a deep net with one (or more) convolutional layers (CNN) and one or more fully connected hidden layers on top.
For deep network with fully connected layers there are methods in theano for unsupervised pre-training, e.g., using denoising auto-encoders or RBMs.
My question is: How can I implement (in theano) an unsupervised pre-training stage for convolutional layers?
I do not expect a full implementation as an answer, but I would appreciate a link to a good tutorial or a reliable reference.

A:

This paper describes an approach for building a stacked convolutional autoencoder.  Based on that paper and some Google searches I was able to implement the described network.  Basically, everything you need is described in the Theano convolutional network and denoising autoencoder tutorials with one crucial exception: how to reverse the max-pooling step in the convolutional network.  I was able to work that out using a method from this discussion - the trickiest part is figuring out the right dimensions for W_prime as these will depend on the feed forward filter sizes and the pooling ratio.  Here is my inverting function:
    def get_reconstructed_input(self, hidden):
        """ Computes the reconstructed input given the values of the hidden layer """
        repeated_conv = conv.conv2d(input = hidden, filters = self.W_prime, border_mode='full')

        multiple_conv_out = [repeated_conv.flatten()] * np.prod(self.poolsize)

        stacked_conv_neibs = T.stack(*multiple_conv_out).T

        stretch_unpooling_out = neibs2images(stacked_conv_neibs, self.pl, self.x.shape)

        rectified_linear_activation = lambda x: T.maximum(0.0, x)
        return rectified_linear_activation(stretch_unpooling_out + self.b_prime.dimshuffle('x', 0, 'x', 'x'))


Q:

Could not find any resources appropriate for the specified culture or the neutral culture

I have two ASP.NET Web projects (ProjectA and ProjectB). When class in ProjectA is instantiating a class of ProjectB which uses a resource file Blah.resx, I get this error:

An exception of type 'System.Resources.MissingManifestResourceException' occurred in mscorlib.dll but was not handled in user code.
Could not find any resources appropriate for the specified culture or the neutral culture.  Make sure "Resources.Blah.resources" was correctly embedded or linked into assembly "App_GlobalResources.sn_flri6" at compile time, or that all the satellite assemblies required are loadable and fully signed.

Whats causing this?
There is an article on Microsoft's site about this http://support.microsoft.com/kb/318603  which suggests:

To resolve this problem, move all of the other class definitions so that they appear after the form's class definition.

This is a solution for Windows Forms project, I'm not sure if that also applies to Web projects.

A:

I just hit this same exception in a WPF project.  The issue occurred within an assembly that we recently moved to another namespace (ProblemAssembly.Support to ProblemAssembly.Controls).  The exception was happening when trying to access resources from a second resource file that exists in the assembly.
Turns out the additional resource file did not properly move references from the old namespace name to the new namespace name.
In the designer.cs for the resource file, there is a static property to get the ResourceManager.  Within that getter, the string was still referring the old namespace.  Once correcting it to the new namespace, the problem was resolved:
global::System.Resources.ResourceManager temp =
     new global::System.Resources.ResourceManager(
          "ProblemAssembly.Support.Properties.Stuff", typeof(Stuff).Assembly);

should have been:
global::System.Resources.ResourceManager temp =
     new global::System.Resources.ResourceManager(
          "ProblemAssembly.Controls.Properties.Stuff", typeof(Stuff).Assembly);

Hope this helps the next person.

A:

I solved the problem like this:

Right click on your ResourceFile
Change the "Build Action" property Compile to "Embedded Resource"
Then build and run

It works perfectly.

A:

When I tried sharing a resource.resx file from one C# project with another C# project, I got this problem. The suggestion on moving the Form class to the beginning of its file wasn't appropriate.  This is how I solved it. You essentially use a link from the second project to the first, then enable regeneration of the resource.designer.cs file.

Delete the second project's Properties/Resources.resx file
Add the first project's Properties/Resources.resx file as a LINK to the Properties folder in the second project. Don't add it to the root level of the project.
Don't add the first project's Properties/Resources.designer.cs!
On the properties of the second project's Resources.resx, add ResXFileCodeGenerator as the CustomTool
Right click on the Resources.resx and select "Run Custom Tool". This will generate a new designer.cs file.

Note: I would avoid editing the resource.designer.cs file, as this is autogenerated.


Q:

retornar string en una funcion

por ej, ingreso 20peso, y 1.80 altura, se obtiene imc<18.50. y quiero que me retorne el string asignado en sobre_peso en la función. que diga en consola, 'Estás sobre peso'.
#include <stdio.h>
char calculoimc(float peso, float altura)
{
    char sobre_peso='Estas sobre peso';
    char estado_salud;
    float imc=0;
    imc = peso/(altura*altura);
    if(imc < 18.50){
        estado_salud=sobre_peso;

    return estado_salud;
            }

}
int main(){
    float p,a;
    printf("Introduzca su peso: ");
    scanf("%f",&p);
    printf("Introduzca su altura: ");
    scanf("%f",&a);

    printf("Su estado es: %s",calculoimc(p,a));
}

A:

El tipo de dato char es un caracter no un arreglo de caracteres o una cadena que es lo mismo, por tanto para representar esos datos debes usar un puntero a char o un string, este es el primer caso:
char* calculoimc(float peso, float altura)
{
    char* sobre_peso='Estas sobre peso';
    char* estado_salud = '';
    float imc=0;
    imc = peso/(altura*altura);
    if(imc < 18.50){
        estado_salud=sobre_peso;
    }
    return estado_salud;
}


Q:

Highcharts - how to add icons to a label

I'm using highcarts to add a label to the centre of my pie chart. I'm using the labels config options but I want to add a label to the beginning of it as such:

I've tried multiple things, including the labelFormatter but nothing seem to work. Currently my code stands as:
labels: {
  items: [{
   useHTML: true, // Using HTML for label
    //html: "Transactions",
    html: '' + "Transactions", // Doesn't work!
    style: {
      fontFamily: 'Arial,Roboto,Helvetica,sans-serif',
      fontWeight: 'bold',
      fontSize: 14,
      verticalAlign: 'middle',
      top: 140,
      left: 460
    }
  }]
},

Please see the JS Fiddle here: http://jsfiddle.net/tobitobetoby/1fqvzpdn/22/

A:

labels.items doesn't seem to support images.
You can use renderer to create the label with the exact inner HTML that you pass as an argument. Notice that useHTML option (7th argument) is enabled. Then you can style it via CSS.
this.renderer.label("

         Transactions
", 120, 125, null, null, null, true).add();

Demo: http://jsfiddle.net/kkulig/p4nnxjq1/
API reference: http://api.highcharts.com/highcharts/Renderer.label


Q:

Confusion regarding discrete signal value?

I know that discrete signals have finite precision. But does that means that those signals can have only integer values(for example,2,3,4 etc) but not decimal values(for example 2.1,3.2,4.2 etc)?

A:

They can very well have decimal values, represented as either fixed point or floating point numbers.

A:

There might be a confusion in the discreteness.
It can apply to the ordinal variable of the data: time for signals, space for images. Here discrete is the opposite of continuous. Which is not often well-defined. A common way is to consider that samples are finite in quantity, or enumerable: you can index them in $\mathbb{Z}$. For irregularly-sampled signal, we are in a borderline case.
For instance, a classically discrete signal $x[n]$, $n\in\mathbb{Z}$ is considered discrete (in time).
But their values (the quantities attached to $x[n]$) might be continuous in nature, e.g. $\mathbb{R}$. This is generally the context of the standard linear signal processing processing (because quantization is typically non-linear).
Then, a signal or, continuous or not in time or space, might have discrete, or discretized values (related to quantization). The values can be classical amplitudes,  letters, or categories (oranges, apples, strawberries).
Classically, practical real-valued signals have bounded amplutude or magnitude. So they often take values in a finite set or a finite field (in communications): $[0,\ldots,2^b-1]$, $[-2^{b-1},\ldots,2^{b-1}-1]$. Yet, even with IEEE floating-point, values remain discrete, because they are finitely many.
So, I suspect that for most, a fully discrete signal would have equally-spaced time samples, and finitely equally-spaced values (potentially with a compander).


Q:

Get all indices of an N-dimensional array as a list

Is there a way to get a list or array of all indices in an N-dimensional array in a fast and efficient way in Python?
For example, image we have the following array:
import numpy as np

test = np.zeros((4,4))

array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])

I would like to get all element indices as follows:
indices = [ [0,0],[0,1],[0,2] ... [3,2],[3,3] ]

A:

Use np.indices with a bit of reshaping:
np.indices(test.shape).reshape(2, -1).T

array([[0, 0],
       [0, 1],
       [0, 2],
       [0, 3],
       [1, 0],
       [1, 1],
       [1, 2],
       [1, 3],
       [2, 0],
       [2, 1],
       [2, 2],
       [2, 3],
       [3, 0],
       [3, 1],
       [3, 2],
       [3, 3]])


Q:

longblob base64 image from db

I am getting my image as a longblob from my mysql database, I got a bit of my code for you. This works in chrome but in IE8 the image do not render properly, or the img cannot be found.

';

A:

IE8 doesn't like data URLs that are over 32k characters long. Is your URL longer than that?


Q:

Printing an RSA private key doesn't actually return it

I'm trying to generate a public/private keypair to use for SSH in Java, this is just a test class to make sure I'm generating the right kind of key.
However, when I go to print the private key to console, it doesn't print the full private key.
Here's my sourcecode:
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.spec.InvalidKeySpecException;

public class Crypto
{
    public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException
    {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");

        keyGen.initialize(1024);

        KeyPair test = keyGen.genKeyPair();

        System.out.println(test.getPrivate());
        System.out.println(test.getPublic());
    }
}

I'm running Java JDK 1.7_45, this is the full console output when run within eclipse:
sun.security.rsa.RSAPrivateCrtKeyImpl@2bd00
Sun RSA public key, 1024 bits
  modulus: <removed because it's so long>
  public exponent: 65537

The string after the @ symbol keeps changing, however I don't believe it to actually be the private key.
Thanks in advance for any help guys!
EDIT: I'm sorry guys I didn't actually ask my question, I simply stated my problem.
My full question is: How do I access the private key as a regular String?

A:

You can cast the private key to the correct implementing type, in this case RSAPrivateKey (and even RSAPrivateCrtKey) and then extract the individual BigInteger components and format them in any way you need.
Example:
KeyPair test = keyGen.genKeyPair();
RSAPrivateKey priv = (RSAPrivateKey) test.getPrivate();
System.out.println(priv.getModulus());
System.out.println(priv.getPrivateExponent());


Q:

Meaning of the phrasal verb "turn around" in context

I stumbled upon this in this video. It is at 6 minutes and 16 seconds.

The next type of akwardness is maybe one of the most common and this one that you want to turn around when you don't know what to do in a conversation.

I guess it means to prevent, but I am in doubt because none of the dictionaries I checked it with doesn't give the definition.

A:

First off, Hollywood types may have such limited cognitive function that trying to learn English from them is about as useful as listening to baboons. Their primary skill is in memorizing words that someone else wrote.
Second, this video is trying to pander to those who are socially and verbally insecure by discussing a performer whose routine is to act as though she has three functional neurons. (I suspect the performer is socially brilliant and very clever, but her act is to appear socially inept in ridiculous ways.)
Third, spoken English frequently requires high levels of interpretation. People speak as they think, and, without an opportunity to parse their words carefully, their utterances frequently do not make literal sense.
The topic of this video is how to carry on an extended phatic conversation. The point is that each party must give a basis for the other party to make a response, to provide the other party an opportunity to make an obvious and socially innocuous rejoinder, to "turn the conversation around."


Q:

xcode with boost : linker(Id) Warning about visibility settings

I have been using a boost framework from the link below for my iPhone Xcode project:
https://goodliffe.blogspot.com/2010/09/building-boost-framework-for-ios-iphone.html
it works fine but I always get hundreds of Apple Mach-O Linker(id) Warnings like:

Direct access in __ZN5boost15program_options6detail7cmdline24handle_additional_parserERSt6vectorISsSaISsEE to global weak symbol __ZTVN5boost17bad_function_callE means the weak symbol cannot be overridden at runtime. This was likely caused by different translation units being compiled with different visibility settings.

How to get rid of those warnings in code?
edited:
By set Symbols Hidden by Default = YES, I managed to get rid of most of the warnings but there are 3 more left which won't go away, can anyone tell me why?
edited again:
After a rebuild the remaining 3 warning are gone as well! So my solution did work!

A:

If boost is included by multiple projects, each project must have the same values for
 Symbols Hidden by Default
 Inline Methods Hidden

A:

Doe just figured how to get rid of hundreds of warning like this :
set for an entire target or project with the Symbols Hidden by Default build setting to YES

A:

The linker complains about different visibility settings between your project and Boost.
You can also fix that issue by recompiling Boost with the same compatibility settings.
Just add
cxxflags=-fvisibility=hidden

and
cxxflags=-fvisibility-inlines-hidden

to the bjam command line.


Q:

OpenSSH with public keys from database

Is it possible to fetch the public keys from a database instead of the authorized_keys file?
I would like to use such a setup to manage ssh access to things like git repositories for multiple users without the need to recreate the authorized_keys file every time a public key is changed or added.

A:

I found this question when trying to answer it myself. After some searching and experimentation, I've found a few other options for this. I'm going to skip the part about distributing keys as an alternative since Matt Simmons covered that. Also, I know there are times when that is not good enough. For example, if you are GitHub and have to store millions of public keys against a single user, continuously updating SSH authorized_keys files and keeping them synchronized across potentially dozens to hundreds of edge boxes is not feasible or desirable.
So,

First of all, RedHat (and variants) have a supported patch for OpenSSH that adds the AuthorizedKeysCommand and AuthorizedKeysCommandRunAs options. The patch has been merged upstream in openssh 6.2. To quote from the man page:

AuthorizedKeysCommand
Specifies a program to be used for lookup of the
  user's public keys. The program will be invoked with its first
  argument the name of the user being authorized, and should produce on
  standard output AuthorizedKeys lines (see AUTHORIZED_KEYS in sshd(8)).
  By default (or when set to the empty string) there is no
  AuthorizedKeysCommand run. If the AuthorizedKeysCommand does not
  successfully authorize the user, authorization falls through to the
  AuthorizedKeysFile. Note that this option has an effect only with
  PubkeyAuthentication turned on.
AuthorizedKeysCommandRunAs
Specifies the user under whose account the
  AuthorizedKeysCommand is run. Empty string (the default value) means
  the user being authorized is used.

In my experiments tonight, I found that out of the box, this doesn't work due to default SELinux policies. You can get around this by turning off SELinux enforcement with setenforce 0. Since turning of SELinux is probably a bad idea, instead you can generate the correct policy. In my case, that was as simple as attempting to log in with the AuthorizedKeysCommand option set up in /etc/ssh/sshd_config and then using audit2allow -a -M local && semodule -i local.pp. This basically looks through the audit logs and finds things that were prevented and generates exceptions for them. If you are likely to have other stuff in there that might get whitelisted, you should probably learn more about audit2allow to make sure you get the new policies just right.
There are other various (probably less tested and trusted) patches out there to add similar functionality. For example there is, openssh-script-auth. You can also find the patch that RedHat used and apply that directly. A quick bout of Googling uncovers https://launchpadlibrarian.net/89063205/openssh-5.3p1-authorized-keys-command.patch and https://launchpadlibrarian.net/105938151/openssh-authorized-keys-command.patch which are based on the RH versions but which have been updated for newer versions of OpenSSH.
Patch OpenSSH to perform key lookups directly from some store (e.g. like GitHub and CodeBaseHQ and others have done). GitHub has not open sourced this patch, to the best of my knowledge, but I know in the past I've come across versions for MySQL and PostgreSQL key lookup. I tried to find them again just now but have not had much luck.
There are also some FUSE-based options. For example there is LPKFuse which lets you serve public keys from LDAP by changing the AuthorizedKeysFile location to one on the LPKFuse filesystem. The LPKFuse FS creates virtual files whose content are backed by fields from a directory server.

All in all, I think option #1 is by far the best as it is officially supported by RedHat. Furthermore, it lets you put any logic you like in that script (including talking to a database) in any language you want.

A:

OpenSSH doesn't have this capability, as far as I'm aware. Your best bet may be to have a script automatically regenerate the file nightly (or as frequently as necessary).
Also, you might want to see this question:
A system for distributing SSH public keys


Q:

How to implement java Arraycopy in Swift?

The definition of Java's arraycopy is here:
Java arraycopy
My Java code is below:
public byte[] getRawData() {
    byte[] raw = new byte[value.length + 4];
    //little endian defined by fido spec
    raw[0] = (byte) (tag);
    raw[1] = (byte) (tag>>8);
    raw[2] = (byte) (len);
    raw[3] = (byte) (len>>8);
    System.arraycopy(value, 0, raw, 4, value.length);
    return raw;
}

Here is my Swift code:
public func getRawData() -> [UInt8] {
    var raw = [UInt8(value.count + 4)]
    raw[0] = UInt8(tag)
    raw[1] = UInt8(tag>>8)
    raw[2] = UInt8(len)
    raw[3] = UInt8(len>>8)
    // The place the put swift arraycopy
    return raw

}

There is no similar function to arraycopy in Swift. How can I properly reimplement this function?

A:

You could use Swift's slice to achieve the same results as Java's arraycopy:
var src = [0, 1, 2, 3]
var dst = [4, 5, 6, 7, 8, 9]

dst[1...4] = src[0...3]

print(dst) // [4, 0, 1, 2, 3, 9]

Note: This does not handle bounds checking.


Q:

Modify iOS view while app is in background or in multitask view

I am writing an iOS app that keeps and displays the state of a system.  It basically shows a light whose color represents state:
green/yellow/red light <==> OK/warning/danger
I'd prefer to show accurate state even when the app is backgrounded, and its snapshot is viewed thru the iOS multitask viewer (the view that looks like a rolodex, which you get after double-clicking the home button).  I REALLY don't want to show a snapshot which displays an inaccurate state.
So my question is:  can you change the view (or perhaps the app snapshot) while the app is in the background, so that when it's viewed in the multitask view it shows an accurate state.
I've seen that this is not recommended by Apple:  from App Programming Guide for iOS

Avoid updating your windows and views.   Because your app’s windows and views are not visible when your app is in the background, you should avoid updating them. The exception is in cases where you need to update the contents of a window prior to having a snapshot of your app taken.

However, Apple's music app does just this...if you start playing a playlist, then put the music app into the background, the cover art gets updated as the music app works thru the playlist.  So this functionality must be possible somehow.
The best answer I can come up with after a bunch of searching is to alter my app's view to the splash image prior to entering background operation.  This would ensure that inaccurate state isn't displayed, but also prevents showing accurate state.

A:

You can change the snapshot view immediately before the snapshot is taken (A banking app, for example might want to obscure personal information), but you cannot continue to update the snapshot from the background once your app is suspended.  You may want to adopt this approach in order to avoid displaying an inaccurate state; show nothing rather than stale data.
You might also want to consider some other options, such as a "today" widget that contains the updated status or posting local notifications when there is a significant change in status (such as entering the "red" state).


Q:

Filter friend picker view controller in ios

I want to filter the facebook friend picker controller to show only friends who have the application. Which api do i use to check if a user has authorized the application ?

A:

The Graph API User object has the installed field, and the FQL User table has the column is_app_user.
If you want to filter the friends yourself, you can go with the API; if you want to only get the already filtered list, then use FQL.


Q:

How to change the default separator of my list items?

In my style.css I have:
.navbar-default .navbar-nav > li > a {
    color: #fff;
    border-right: 1px solid rgba(0,0,0,.2);
    box-shadow: 1px 0 0 rgba(255, 255, 255, 0.10);
}

This causes my menu to display:
Home  |  About  |  Blog  |  Shop  |
I would like it to display:
Home  /  About  /  Blog  /  Shop
Does anyone have any hints for me? I tried:
.navbar-default .navbar-nav > li > a {
   color: #fff;
   content: "/" rgba(0,0,0,.2);
}

But that just made the separators disappear altogether.

A:

You don't need position:absolute at all as it's overkill (albeit not very much overkill). You can use floats instead:

li {
    display: inline-block;
    margin-left:10px; /* Adjust as needed */
}
li:not(:first-of-type):after { /* Exempts the first LI */
    display:block;
    float:left;
    margin-right:10px; /* Adjust as needed */
    content:"/";
}

Home

Show

About

Contact


Q:

GraphQL relationship in Mongoose

I have the following GraphQL Schema
type User {
  id: String!
  name: String
  username: String!
}

type Conversation {
  id: String!
  participants: [User]
}

type Query {
  user(_id: String!): User
  conversation(_id: String!): Conversation
}

My resolver for the conversation is as follows:
conversation: async (parent, args) => {
  let conversation = await Conversation.findById(args._id);
  conversation.id = conversation._id.toString();
  return conversation;
}

The participants field will hold array of users ObjectId. What do I need to do in my resolver so I can fetch users data within the conversation call.
For example a call like this
query test($id:String!){
  conversation(_id:$id){
    id,
    participants {
      id,
      username
    }
  }
}

A:

You probably used Reference in your object model, therefore in order to fetch participants data, you should use mongoose populate!
This would work for you:
conversation: async (parent, args) => {
  let conversation = await Conversation.findById(args._id).populate('participants');
  conversation.id = conversation._id.toString();
  return conversation;
}


Q:

Making a query for all objects that don't have `None` for a certain field

I have a model Chair with a nullable field .datetime.
I want a query that will get all chairs that have some value in .datetime, no matter what it is, as long as it's not None.
I know how to do this by building a Q object and negating it, but is there a less cumbersome way?

A:

Chair.objects.filter(datetime__isnull=False)

It's interesting you mention negating the Q object - people usually learn the other way around :)


Q:

Where does Lollipop stores notification log?

Android 4.3 and above allows access to a list of historical notifications using the Settings widget. The scope of "historical" is limited to current session of Android and the list is populated with new entries once the system is rebooted.
(Click image to enlarge)

So, where does this notification log is stored in Android?

I had no luck finding anything good named "notification". I already did a system-wide search with nothing positive to report here.
find / -iname "*notification*"

I watched the real-time output of adb logcat -v long (tried with explicit root access as well) but there was no common event  when a notification popped up in notification drawer. I was monitoring the ping request (shows up as a notification) to KDE Connect sent from my Slackware Linux, for the test case.
The entries that are visible in Notification drawer can be traced back to the output of notification service,
adb shell dumpsys notification

As said, you get the current ones but not the historical ones.
The real name or codename (I suppose) of notification service is android.app.INotificationManager.   I attempted to dive into its  source
code and there is nothing positive to report. It is very likely that being not a programmer, I might have missed a thing or two of importance.
I, while being reluctant for being illogical, did peek into the data directory of Settings (com.android.settings) and Settings Storage (com.android.providers.settings) apps. Nothing interesting there.

I presume that either the log is managed in the memory by a program or at some location in storage where the logs are frequently flushed after current Android session ends. I've no idea what I'm talking here.
Edit:
I've the Notification access to Tasker under Settings → Notification manager already enabled. So that's definitely not the roadblock here.
(Asked by Lucky) The intent associated with that Notification log window accessible through Settings widget has the following things:

Action: android.intent.action.MAIN
Category: com.android.settings.SHORTCUT
Flag: 0x10200000
Component: com.android.settings/.Settings$NotificationStationActivity

Note

I've a rooted OnePlus One running COS12 (Xposed Framework installed) and many Android 5.1 ROMs.
I'm absolutely aware about many apps which can record notifications and lets you see them. But as you would've guessed by now, I'm not interested in futuristic past but the past related to present moment. In short, I want access to native raw files or logs to see both the historical and current notifications without depending on third-party apps.

My goal
When a secondary user is in focus, Tasker (running under Primary user) misses any profile which depends upon an incoming notification. Google has noted the fact here:

Notifications for other users do not appear until they are active.

But you can still see those new notifications in the output of adb shell dumpsys notification. So I want to access the logs directly to make my task(s) work, thereby removing the dependence on notifications showing up in Notification drawer.

A:

The historical record is stored in memory, and is not directly available in the service API (as far as I'm aware). The Settings activity accesses it through the NotificationService.getHistoricalNotifications() method, but this method is not exposed in the NotificationManager API (only getActiveNotifications() is). You might be able to do it through reflection (not tested) but you definitely will not be able to find a file with this information. My understanding of most apps that display these records is that they actually keep their own list somewhere, and add new items as they are posted. Hence, they would not be able to access your historical record from before the time of installation.
All of the logic for managing the queue is in the NotificationManagerService$Archive inner class. The gist of it is that when a new notification comes in, the system sends it to the the service which then calls Archive.record(notification). By default, AOSP has the configuration set so that the Archive will hold a maximum of 100 records, then it will start popping the front item from the queue each time a new one is added.
Your only real option here is likely going to be polling the output of either adb dumpsys or the NotificationManager.


Q:

Compare lists in python while looping

I have a script which I'm using to read an excel file and update an SQL database. I'm reading the excel file every 30 seconds using a loop. However I only want to update the database when the excel file changes
If I use the != operator when the loop cycles it  refreshes the value of 'temp' and thus does not register that the value is the same.
Does anyone have an idea how to solve this problem..?
Thanks!
edit: updated to make my problem more clear!
def update():
    threading.Timer(1, update).start()
    book = open_workbook('bet.xls')

    def odds():
        sheet = book.sheet_by_name('xyz')
        match_sheet = sheet.cell(5,0).value
        data = book.sheet_by_name(sheet)
        vv = data.cell(3,26).value

        temp= None

        if vv != temp:
            print 'hello'

        temp= vv

odds()

update()

A:

Yes, Python built-in containers are compared by value (both tuples, lists and dicts).
Something like this (I used a list comprehension to add fanciness):
//init
pvv=None

<...>

//iteration
vv= [data.cell(i,j).value for (i,j) in ((2,26),(3,26),(4,26))]
if vv!=pvv:
    //do something
    pvv=vv


Q:

Bootstrap CDN + WP // How to edit '.css’

I’m building my website on Wordpress + Bootstrap CDN.
I decided to make it on CDN to have no problem with updates in the future plus I read that this way it’s a bit faster.
So the thing is I have a problem with styles.
I imported my local ‘style.css’ to ‘header.php’, but since ‘bootstrap.min.css’ from CDN also has its own parameters I can’t apply some things.
How do I rewrite CDN’s parameters? Or is there a way to edit this exact  ‘bootstrap.min.css’ file?
Thanks in advance.

A:

You can simply just use wp_enqueue_style to load those files. Here is an example you can get an idea from.
function enqueue_my_scripts() {
wp_enqueue_script( 'jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js', array('jquery'), '1.9.1', true); // we need the jquery library for bootsrap js to function
wp_enqueue_script( 'bootstrap-js', '//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js', array('jquery'), true); // all the bootstrap javascript goodness
}
add_action('wp_enqueue_scripts', 'enqueue_my_scripts');

function enqueue_my_styles() {
wp_enqueue_style( 'bootstrap', '//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css' );
wp_enqueue_style( 'my-style', get_template_directory_uri() . '/style.css');
}
add_action('wp_enqueue_scripts', 'enqueue_my_styles');

and to write the rules just use !important in your style.css file.
When you use wp_enqueue_style, you have the option to take control on where your styles and script be used for further references. Here is an example how to use it.
function my_enqueue_stuff() {
  if ( is_page( 'landing-page-template-one' ) ) {
    /** Call landing-page-template-one enqueue */
  } else {
    /** Call regular enqueue */
  }
}
add_action( 'wp_enqueue_scripts', 'my_enqueue_stuff' );