Sunday, March 20, 2011

Is there a way to import blockbuster.com queue into Netflix?

Is there a utility or website that takes the all of the movies in a blockbuster.com queue and adds the corresponding movies (blu-ray and all) to Netflix.com?

From superuser
  • While Netflix has a published API for manipulating your account, doing searches, etc, Blockbuster doesn't seem to have such a thing. However!

    You can save an HTML template of your Blockbuster queue, and then use Netflix's API to work with your queue there.

    This blog post describes an outline of doing this, and links to a php program that is in early stages of development.

Make files dump into a fence by default

I want to make any files that get dumped onto my desktop go into an "Unfiled" or "Misc" fence by default with Fences, is there a way to do this?

From superuser
  • I wish this existed as well, but unfortunately auto-fencing is not yet supported. It doesn't hurt to send in a request for it though!

    Ivo Flipse : That would be even more awesome ;-)

PHP - Extremely light templating system

Hi

does anyone know a php templating system that is very simple, something like almost as simple as str_replace("{variable}", $variable); ?

I need this for a series of textareas in the administration panel, where the site admin should change templates for various elements of the website (not complex stuff like pages etc, just blocks of content)

From stackoverflow
  • /**
     * Renders a single line. Looks for {{ var }}
     *
     * @param string $string
     * @param array $parameters
     *
     * @return string
     */
    function renderString($string, array $parameters)
    {
        $replacer = function ($match) use ($parameters)
        {
            return isset($parameters[$match[1]]) ? $parameters[$match[1]] : $match[0];
        };
    
        return preg_replace_callback('/{{\s*(.+?)\s*}}/', $replacer, $string);
    }
    
    Emil H : Neat. :) It should be said that it's php 5.3+ only, though.
    alex : You'll need to be running PHP 5.3 to use this. Otherwise it is not so elegant.
    willell : Try to avoid this if you can. Regexes are resource-expensive.
    Gordon : Same as Fabien Potencier's in [Design patterns revisited with PHP 5.3](http://www.slideshare.net/fabpot/design-patternrevisitedphp53) (page 45)
    efritz : It's actually a method from the Symfony2 framework.
    Alex : nice. is there a way to make this work for php 4 ?
    alex : @Alex You'll need to make a separate function for the callback (outside of the scope you are currently in).
    Gordon : @efritz figures because Fabien is the author of Symfony2
  • $findReplaces = array(
        'first_name' => $user['first_name'],
        'greeting' => 'Good ' . (date('G') < 12 ) ? 'morning' : 'afternoon'
    );
    
    $finds = $replaces = array();
    
    foreach($findReplaces as $find => $replace) {
        $finds[] = '{' . $find . '}';
        $replaces[] = $replace;
    }
    
    $content = str_replace($finds, $replaces, $content);
    
    Gordon : why dont you just include the curly braces, e.g. `{{first_name}}` in the array keys and simply do `str_replace(array_keys($findReplaces),$findReplaces,$content)`

Truncate ASP.NET MVC C#

I'm displaying some content using the following bit of code:

<% foreach ( var m in ViewData.Model) { %>

<div class="content">
<%= m.article %>
</div>

Which shows a news article from my database. What I want to do is truncate the story to be smaller, how do would I do this? Also the article column contains HTML tags such as <p> so therefore splitting up the article purely on character numbers would cause issues.

Any ideas on where to start? I'm new to MVC.

Thanks

From stackoverflow
  • This is not a simple problem, but you can start by using the HtmlAgilityPack.

    This is an agile HTML parser that builds a read/write DOM and supports plain XPATH or XSLT (you actually don't HAVE to understand XPATH nor XSLT to use it, don't worry...). It is a .NET code library that allows you to parse "out of the web" HTML files. The parser is very tolerant with "real world" malformed HTML. The object model is very similar to what proposes System.Xml, but for HTML documents (or streams).

    Once you have your DOM in memory you can then extract or truncate elements in a way that will not break the DOM structure, e.g., you can remove entire <p></p> nodes, or just truncate some fo the text within that node.

    Cameron : Hi, I don't want to download any additional stuff. I want to use purely the default framework.
    RedFilter : The default framework does not contain anything to handle the messiness that is HTML. If you know that your content is valid XHTML (or you can convert it to be), then just use the XML classes in C# to do something similar.
    Cameron : Yes it is valid XHTML. Would you be able to explain how to do that. As like I said I'm new to C# and MVC and so not really sure what you mean.
    Paco : Or copy paste the code from the library you don't want to download.
    RedFilter : @Paco: HtmlAgilityPack is provided as source.
  • Do you have control over how articles are submitted? Perhaps you can ask for a short intro/summary. I believe WordPress does it.

    Or have the user insert a marker which signifies the end of intro/summary. something like:

    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    <!-- END INTRO -->
    <p>Ut malesuada porttitor consectetur. </p>
    

    You can display it without any consequences and still "split" the article programmatically.

Javascript select drop down menu onchange not firing properly

my onchange isn't firing the javascript method properly.

Select code (in HTML file)

<select id="crit_1" onchange="crit1Update()">
    <option value=null>Criteria 1</option>
 <option value="major">Major</option>
 <option value="grad">Graduation Year</option>
</select>

crit1Update() code (in external filter.js file in same folder as HTML file containing the above with the following code in head of HTML

<script type="text/javascript" src="filter.js">
</script>

function crit1Update() {
 var crit1 = document.criteria.getElementsByID("crit1");
 var criteria1 = crit1.options[crit1.selectedIndex].value;
 alert("criteria1"); // this is never firing, so this isnt getting called
 if (criteria1 == null) {
  // eventually set all other lists to null in here as well
  filter(null,null,null,null,null,null)
            // the above returns all the resumes 
            // (this is eventually going to filter resumes)
 }
 else if (criteria1 == "major") {
  alert("Major!");
 }
}

Are there any glaring or not glaring errors that I am missing? I dont see why the alert("criteria1" isnt being called. I come from a java background and this is my first foray into JS, so I may be missing something simple.

Thanks for any help.

From stackoverflow
  • Your <select> ID is "crit_1" and your function is looking for ID "crit1" (no underscore).

    It would probably be a better idea to pass the <select> element as a reference to the function, eg

    JavaScript

    function crit1Update(crit1) {
        var criteria1 = crit1.options[crit1.selectedIndex].value;
    

    HTML

    <select id="crit1" name="crit1" onchange="crit1Update(this)">
    
    Ross Larson : thank you very much!
    klox : @Ross: read some tutorial for the project that do you want to built up. so it can prevent the mistake during type the script.
    Ross Larson : @Klox, that is probably a good idea. I have been going back and forth between my js doc, html doc and www.w3schools.com
    klox : yah w3schools is a place for beginner like me too.keep trying and learning.;)
  • There is a typo in this method call:

    document.criteria.getElementsByID(
    

    should be

    document.criteria.getElementById(
                                ^  ^ no "s" and lower case "d"
    

    also make sure the id matches your HTML element e.g. "crit_1"

    Phil Brown : Good catch on the first typo, but you also missed the "s" - `getElementsByID` should be `getElementById`
    Ross Larson : Thank you very much
    scunliffe : thanks @Phil Brown I was so focused on the "D" I missed the s.

How to return a data.frame with a given name from a function?

Assume I have a function that reads data from a MySQL table, manipulates it and returns some data.frame. Note the function is just an example whose functionality does not matter itself..., E.g.:

addRowSd <- function(table,con,pattern="^Variable") {

dframe <- dbReadTable(con,table)
cn <- colnames(dframe)
qs <- subset(x, x  %in% grep(pattern, x, value=TRUE))
dframe$qsd <- sd(t(dframe[,c(qs)])) 

return(dframe)
}

mydf$sd <- addRowSd(...)

I end up with a data.frame called mydf. Now I´d like to do to this to a character vector of SQL table names AND name the returned dataframes correspondingly. If I just use

x=lapply(MySQLtablenames,addRowSd,con)

I´ll get some list called x. Of course I could unlist and rename everything the way I´d like to, but my question is:

How can I make lapply (or another comparable function) return multple single dataframes or at least a list that contains some names derived from my character vector "MySQLtablenames"?

From stackoverflow
  • just found an answer on my own:

    assign("somename",dframe,envir = .GlobalEnv)
    
    Marek : You could run into scoping issues with this. E.g if you want to use function inside another function, then your data.frame is created in global environment. You could play with `parent.frame` to improve your method.
    Gavin Simpson : In both your examples, you are inserting a DF into a list (or `lapply` returns a list). In those cases, the name of the object that is the DF is irrelevant, is it not. What matter is the `names` for the list components. If I were doing this, I'd store the DFs in a list, set the names on the list after the table names, as per Marek's comment above, and then work with the list. If you want to refer to the individual DFs by name, use `with(x, DF.name)` where `x` is list returned by `lapply` and `DF.name` is the name of the DF you want to access or some function applied to it.
    ran2 : @Marek, currently the only thing that runs into scoping issues is my brain. the parent.frame stuff is all new to me and I do not get it yet, but thx for pointing to it. What will cause the scoping issues? Loading to much data from the database to the same (global) environment?
  • If you supply sapply a character vector, it will name the items in the returned list by the supplied character vector (USE.NAMES default to TRUE)... I would also use simplify=FALSE as depending on the data.frames returned you may get unpredictable results

    x=sapply(MySQLtablenames,addRowSd,con, simplify=FALSE)
    

Best python/Django code generator?

Hello, very quick question. I am wondering if there are any software Django software that generates python coding automatically. If there are any, please let me know.

From stackoverflow
  • I have never heard of python code generators. Python is a dynamic language where you can do almost anything. Instead of relying on static code generators known from the Java world, I would argue that you can write a couple of equivalent Python lines in most cases which is way more convenient.

    In case you are looking for a Python lexer/parser, try pyparsing.

    Shehzad009 : Ok I will check it out