Monday, April 25, 2011

Checking Download size before download

Hi

I need some way to check the size of a download without having to download the entire file. I am using C# and the System.Net.WebClient to do the downloads.The check needs to run in a asp.net webservice.

Thanks

From stackoverflow
  • Make a HEAD (rather than GET or POST) request to just get the response headers, this should include the content-length header with the information you need.

  • Use HTTP method HEAD to retrieve Content-Length: header.

    HEAD / HTTP/1.1
    Host: www.example.com
    
    HTTP/1.1 200 OK
    Date: Wed, 18 Mar 2009 11:21:51 GMT
    Server: Apache/2.2.3 (CentOS)
    Last-Modified: Tue, 15 Nov 2005 13:24:10 GMT
    ETag: "b80f4-1b6-80bfd280"
    Accept-Ranges: bytes
    Content-Length: 438 
    Connection: close
    Content-Type: text/html; charset=UTF-8
    
    RC1140 : Hi you can use the code at this page to actually get the details (for anyone that needs the info) http://www.eggheadcafe.com/tutorials/aspnet/2c13cafc-be1c-4dd8-9129-f82f59991517/the-lowly-http-head-reque.aspx
    Jim Mischel : Assuming that the Content-Length header is there. You'd be surprised how many sites don't supply that information, and how many don't even support HEAD.
    RC1140 : What would you use as an alternative
    vartec : Actually in these cases there is no alternative. Luckily these are rather rare cases. For example if you download stuff from one of these servers, your browser instead of giving you normal progress bar and ETA, gives you indeterminate progress bar or spinner.
  • You can also use the HTTP RANGE header to download only the stuff you want.

    It would be really simple to build a HttpRangeStream that supports seek and read on a remote HTTP resource, if the remote server is HTTP 1.1 and correctly supports RANGE headers.

I am looking for a content addressing data structure

I'm trying to design a data structure that allows efficient extraction of entries from a part of their content.

Let's say I am looking for an entry that matches this: [ x 2 3 x x ]

If [ 0 2 3 4 5 ] or [ 3 2 3 7 8 ] are in my data structure, they should be returned by my find function.

I wrote such a data structure where I compare the "pattern" against all entries of the data structure, but of course it takes way too much time. I have few ideas about how to do this a faster way but they are quite heavy to implement. Does something like this already exists? If not, how would you do this?

From stackoverflow
  • What you're trying to implement looks a lot like tuple space.

    Ben : Yes it is actually what I want to do but I need I wanted a hint for implementing it. I guess I'll to look at the code! Thanks
    vartec : Let me guess. Distributed programming class homework? ;-)
    Ben : Nop ! that is real work :/
    vartec : Oh, then why not use already available solutions?
    Ben : Well I have specific needs but I simplified a bit the problem to ask my question. Available solutions are not exactly what I want, but they are a good start for implementation, I am on it.
  • Well, a Suffix tree could do it in O(1), but it would take a LOT of memory.

    j_random_hacker : +1. Assuming you have multiple entries, what you really want is called a generalised suffix tree, which is formed by concatenating all the entries together, using distinct (unique) separator symbols between each. Suffix arrays are a (theoretically) slower but much more memory-efficient alternative.
    j_random_hacker : P.S.: Suffix trees and arrays rock, but they are *hard* to implement. If you can't find an existing implementation, give yourself a solid 2 weeks to try to understand Ukkonnen's or McCreight's original papers on linear-time construction algorithms.
    Darius Bacon : How specifically does it do it in O(1)? You can retrieve all the entries containing a particular substring in O(length(substring)), if that's what you mean. I think the given problem does not restrict the known bits to be a single contiguous substring, either.
    Vilx- : I think it does restrict it to a single contiguous substring, but yes, it's not very clear on this. As for O(1) - well, it depends on how you look at it. The Big O in this case depends only on the length of the substring, not the amount of entries in the dictionary.
    Vilx- : Clasically when one talks about searching/sorting algorithms he measures the Big O in respect to the number of entries, not the length of them. From that point of view it's O(1).
  • The first thing that comes to mind is to have a hash table for each position in the tuple. For searching you intersect the results for all the positions with a specified value.

    dmeister : To hash each item is a starting point, but for large answer sets the intersection takes time and consumes memory. Consider also to hash each subset of a specific window size.
  • You might take a look at the RETE algorithm. This general problem came up in the AI systems of the 70s; Chapter 14 in Paradigms of AI Programming covers it. (IIRC there's mainly elaborations of starblue's answer and decision trees.)

Can a request be handled and ended prematurely, early in the pipeline?

I have an HttpModule that has bound an event handler to EndRequest.

Is there any way to handle the request inside the event handler? Meaning, I don't just want to run code and keep the request moving -- I want to stop it dead in its tracks, return a 200 Status Code, and call it a day, without it request continuing to the next step in the pipeline.

From stackoverflow
  • HttpContext.Current.ApplicationInstance.CompleteRequest();

    Deane : This looks interesting, but will this kill the request entirely? I appears that ti just skips the rest of the pipeline and goes straight to EndRequest. However, that's where I'm at already (that's the event I bound my handler to), and doesn't it still to provide a response?
    Robert C. Barth : This will end all further processing of the request. Your handler is not the only handler in the pipeline, so this will cancel further processing of other handlers after yours and send the response immediately to the client.

jQuery-UI Theming - CSS Sizing Differences

When using the jQueryUI samples on the website (theme browser) things look great. But, when I put the code and theme into my application, the sizing is way out of whack (I'm trying to utilize the Redmond theme).

Any ideas as to why these samples look so different? In the application I've built, there is only 1 CSS reference which is for the Redmond theme... These images below are exactly how they were rendered to the browser.

My Application:
alt text

jQuery UI Sample:
alt text

My Application Code:

<div>
    <asp:TextBox ID="txtDateSample" runat="server"></asp:TextBox>
</div>
</form>
<script type="text/javascript" language="javascript">
    $(function() {
        $('#<%= txtDateSample.ClientID %>').datepicker({
            showButtonPanel: true,
            showOn: 'button',
            buttonImage: '../Graphics/Icons/calendar.png',
            buttonImageOnly: true
        });
    });
</script>

Firebug View of Div:

<div id="ui-datepicker-div" class="ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all ui-helper-hidden-accessible"/>
From stackoverflow
  • This used to happen to me. It is because some of your styles are inadvertently affecting the calendar div. Get Firebug and check out what styles are being applied to the calendar. Then edit the theme to override anything your styles are doing.

    EDIT:

    The part you are looking for is not the HTML generated, although that is important, it is more of what styles are actually being applied:

    Firebug Style Tab

    You will see styles that are being applied while styles that have been assigned but are not being applied will be shown struck out. You will probably see some of your styles in this window. They are most likely the culprit. Fix them by making them more specific and your calendar should be good to go.

    RSolberg : I wonder if that is why they provide the "edit theme" functionality. Modified the font size and that seemed to do the trick.
    Paolo Bergantino : Ah, good. I guess my edit was for nothing then. :)
    RSolberg : Thanks. I'll look at this again later tonight. I believe it was the font size setting on the theme. 1.1em vs. small.
  • When things are smaller than you expect, a good assumption would be that some kind of percentage/proportional sizing is happening recursively, i.e. that you've got

    .foo {width: 80%;}
    

    somewhere and that has been applied twice or more, meaning that your width ends up at 80% of 80% of 80%.

In Haskell how do you extract strings from an XML document?

If I have an XML document like this:

<root>
  <elem name="Greeting">
    Hello
  </elem>
  <elem name="Name">
    Name
  </elem>
</root>

and some Haskell type/data definitions like this:

 type Name = String
 type Value = String
 data LocalizedString = LS Name Value

and I wanted to write a Haskell function with the following signature:

 getLocalizedStrings :: String -> [LocalizedString]

where the first parameter was the XML text, and the returned value was:

 [LS "Greeting" "Hello", LS "Name" "Name"]

how would I do this?

If HaXml is the best tool, how would I use HaXml to achieve the above goal?

Thank!

From stackoverflow
  • I've never actually bothered to figure out how to extract bits out of XML documents using HaXML; HXT has met all my needs.

    {-# LANGUAGE Arrows #-}
    import Data.Maybe
    import Text.XML.HXT.Arrow
    
    type Name = String
    type Value = String
    data LocalizedString = LS Name Value
    
    getLocalizedStrings :: String -> Maybe [LocalizedString]
    getLocalizedStrings = (.) listToMaybe . runLA $ xread >>> getRoot
    
    atTag :: ArrowXml a => String -> a XmlTree XmlTree
    atTag tag = deep $ isElem >>> hasName tag
    
    getRoot :: ArrowXml a => a XmlTree [LocalizedString]
    getRoot = atTag "root" >>> listA getElem
    
    getElem :: ArrowXml a => a XmlTree LocalizedString
    getElem = atTag "elem" >>> proc x -> do
        name <- getAttrValue "name" -< x
        value <- getChildren >>> getText -< x
        returnA -< LS name value
    

    You'd probably like a little more error-checking (i.e. don't just lazily use atTag like me; actually verify that <root> is root, <elem> is direct descendent, etc.) but this works just fine on your example.


    Now, if you need an introduction to Arrows, unfortunately I don't know of any good one. I myself learned it the "thrown into the ocean to learn how to swim" way.

    Something that may be helpful to keep in mind is that the proc/-< syntax is simply sugar for the basic arrow operations (arr, >>>, etc.), just like do/<- is simply sugar for the basic monad operations (return, >>=, etc.). The following are equivalent:

    getAttrValue "name" &&& (getChildren >>> getText) >>^ uncurry LS
    
    proc x -> do
        name <- getAttrValue "name" -< x
        value <- getChildren >>> getText -< x
        returnA -< LS name value
    
    Tim Stewart : Thank you very much for a very informative answer!
    Paul Johnson : There is a HXT tutorial at http://www.haskell.org/haskellwiki/HXT, but it is relentlessly point-free, so understanding how this relates to the arrow do-notation (as in the example above) is not easy.
  • FWIW, HXT seems like overkill where a simple TagSoup will do :)

  • Here's my second attempt (after receiving some good input from others) with TagSoup:

    module Xml where
    
    import Data.Char
    import Text.HTML.TagSoup
    
    type SName = String
    type SValue = String
    
    data LocalizedString = LS SName SValue
         deriving Show
    
    getLocalizedStrings :: String -> [LocalizedString]
    getLocalizedStrings = create . filterTags . parseTags
      where 
        filterTags :: [Tag] -> [Tag]
        filterTags = filter (\x -> isTagOpenName "elem" x || isTagText x)
    
        create :: [Tag] -> [LocalizedString]
        create (TagOpen "elem" [("name", name)] : TagText text : rest) = 
          LS name (trimWhiteSpace text) : create rest
        create (_:rest) = create rest
        create [] = []               
    
    trimWhiteSpace :: String -> String
    trimWhiteSpace = dropWhile isSpace . reverse . dropWhile isSpace . reverse
    
    main = do
      xml <- readFile "xml.xml"  -- xml.xml contains the xml in the original question.
      putStrLn . show . getLocalizedStrings $ xml
    

    The first attempt showcased a naive (and faulty) method for trimming whitespace off of a string.

    ephemient : TagSoup happily accepts malformed input -- which you might actually like :) -- unfortunately IMO this solution is harder to read. Minor nit: I'd have expected something more like `trimWhiteSpace = dropWhile isSpace . reverse . dropWhile isSpace . reverse`; yours is more like `removeAllWhiteSpace`.
    Tim Stewart : Thanks ephemient. I should have had some better sample data. :) I'll have to make sure that isSpace gets rid of newlines because I had some newlines embedded in my XML.
    ephemient : Just try for yourself: type `Data.Char.isSpace '\n'` into GHCi. Yes, newlines are, and have always been, whitespace. My nit wasn't about that, more along the lines of your `trimWhiteSpace " a b c " == "abc"` which is non-intuitive to me. Or maybe I'm strange.
    Tim Stewart : You're absolutely right. I want to keep those internal spaces. Thanks.
  • Use one of the XML packages.

    The most popular are, in order,

    1. haxml
    2. hxt
    3. xml-light
    4. hexpat

Age from Date of Birth using JQuery

I need to calculate if someone is over 18 from their date of birth using JQuery.

var curr = new Date();
curr.setFullYear(curr.getFullYear() - 18);

var dob = Date.parse($(this).text());

if((curr-dob)<0)
{
    $(this).text("Under 18");
}
else
{
    $(this).text(" Over 18");
}

There must be some easier functions to use to compare dates rather than using the setFullYear and getFullYear methods.

Note: My actual reason for wanting to find a new method is length of the code. I have to fit this code into a database field that is limited to 250 chars. Changing the database is not something that can happen quickly or easily.

From stackoverflow
  • You could use the Date object. This will return the milliseconds between the two dates. There are 31556952000 milliseconds in a year.

    function dateDiff(var now, var dob) { return now.getTime() - dob.getTime(); }

    Garry Shutler : Surely there aren't that many milliseconds every year. Dates are hard.
    Jon Erickson : that is not always true (leap years)
  • $(this).text(((new Date().getFullYear()-Date.parts($(this).text()))>=18)?"Over 18":"Under 18");
    

    Better? :D

    Parrots : This only seems to use the year field for calculation, won't take into account months/days of current date and the DOB. So with this someone born in Dec 1991 would come back as over 18 in Jan 2009, which isn't true (they'd be 18 in Dec 2009).
    Sudhir Jonathan : yes... it won't take into effect the months and days.. using a library would be useful if you want the full functionality, like in the first answer. Still can be built, though it might be difficult inside of 250 characters.
  • You can remove the separate variable for DOB and collapse the if statement. The below code comes in at 165 characters:

    var check = new Date();
    check.setFullYear(check.getFullYear() - 18);
    $(this).text((new Date("3/6/2009").getTime() - check.getTime() < 0)?"Under 18":"Over 18");
    

    This will still keep the logic needed to deal with leap-years.

  • You might find the open source Datejs library to be helpful. Specifically the the addYears function.

    var dob = Date.parse($(this).text());
    if (dob.addYears(18) < Date.today())
    {
        $(this).text("Under 18");
    }
    else
    {
        $(this).text(" Over 18");
    }
    

    In a more terse fashion:

    $(this).text(
        Date.parse($(this).text()).addYears(18) < Date.today() ?
        "Under 18" :
        " Over 18"
    )
    
    RSolberg : +1 - seems much easier to ready that others. I'm using this library and its been a big help.
  • Date.prototype.age=function(at){
        var value = new Date(this.getTime());
        var age = at.getFullYear() - value.getFullYear();
        value = value.setFullYear(at.getFullYear());
        if (at < value) --age;
        return age;
    };
    
    var dob = new Date(Date.parse($(this).text()));
    
    if(dob.age(new Date()) < 18)
    {
        $(this).text("Under 18");
    }
    else
    {
        $(this).text(" Over 18");
    }
    

Theme a CCK input form in Drupal 6

In my template.php file I've this:

function myadmintheme_theme(&$existing, $type, $theme, $path) {
  return array(
    'node_form' => array(
     'arguments' => array('form' => NULL),
     'template' => 'ccktype',
    ),
  );
}

And I've a ccktype.tpl.php in the same dir:

<b>works!</b>

But is not working, if I enter to node/add/ccktype I don't get the "works!" message.. I tried everything!

any help would be appreciated

From stackoverflow
  • First of all, see that Drupal's not caching your module. Go to Administer > Site Configuration > Performance, and clear all caches.

    If that doesn't work, try renaming the file to node-add-ccktype.tpl.php.

    br1 : thanks, but is not work... it's strange, isn't it?
    Seb : Yeah, I really don't know... I had problems with that theme hook as well. Sorry I can't help anymore :(
  • The theme function you need to override is ccktype_node_form, not node_form. All node forms maintained by the node module get mapped to the node_form function for building, but they still have unique form ids.

    br1 : I tried that also, but didn't work. I forgot to say that in the "Administration theme" y check "Use administration theme for content editing" (that is obvious).
    br1 : hey, I didn't notice this, but there is a very little change in the style when I use ccktype_node_form :D It is not printing "WORKS!", but is better than nothing ;D
    br1 : hey, now works! I love you!!! xD
  • This is the solution:

    function myadmintheme_theme($existing, $type, $theme, $path) {
      return array(
        'ccktype_node_form' => array(
         'arguments' => array('form' => NULL),
         'template' => 'ccktype',
        ),
      );
    }
    

    thanks a lot Eaton!

    Seb : Glad you solved this! If Eaton helped you, then please mark his answer as correct; that will help others find this problem is solved and will grant Eaton reputation points for having helped. :)
  • Thanks so much for this! I spent a ton of time looking on the stupid Drupal site and tried about 20 things that didn't work.

    Ugh!