Tuesday, April 5, 2011

Buffered Multipart Form Posts in Ruby

I am currently using Net::HTTP in a Ruby script to post files to a website via a multipart form post. It works great for small files, but I frequently have to send very large files using this script, and HTTP#post only seems to accept post data as a String object, which means that the file I'm sending has to be read into memory before anything can be sent. This script is running on a busy production server, so it's unacceptable to gobble up hundreds of megabytes of RAM just to send a file.

Ideally, there'd be a method that could be given a buffer size and an IO object, and would send off buffer-sized chunks of data, reading from the IO object only as required. What would be the best way to make this happen? Did I miss something relevant in Net::HTTP?

Update: Net::HTTP#body_stream(input) looks good, though the documentation is rather... sparse. Anyone able to point me to a good example of this in action?

From stackoverflow

Which is more efficient in Haskell; pattern matching or nested if/case statements?

I'm just curious about the efficiency of pattern matching in Haskell. What is a simple case of where pattern matching would be better than nested if/case statements and then the converse?

Thanks for your help.

From stackoverflow
  • I didn't confirm this, but I think both forms will become a nested case-of expression when translated to core Haskell by the compiler. The best way to find out is asking the compiler itself. In GHC you can turn on the dump of the core intermediate program by using the arguments:

    • Before simplifications: -ddump-ds
    • After simplifications: -ddump-simpl
  • In Haskell, case and pattern matching are inextricably linked; you can't have one without the other. if p then e1 else e2 is syntactic sugar for case p of { True -> e1; False -> e2 }. For these reasons, I think it is impossible to produce the examples you ask for; in Core Haskell, everything is equivalent to case.

    In languages in the ML family, the optimizer can often do very impressive things with complex pattern matches. This is more difficult for Haskell compilers; because of lazy evaluation, the pattern-match compiler is not allowed to reorder certain tests. In other words, if you nest case statements in different ways, you may get different performance, but in Haskell you also get different semantics. So generally the compiler doesn't mess with it.

    As far as which way to write your own code, it's safe to assume that the code with the fewest case expressions is the best (keeping in mind that one if is equivalent to one case expression).

    Jon Harrop : Can you elaborate on the laziness issue? Is it to retain performance characteristics?
  • According to the specification, they are semantically equivalent. This, of course, does not necessarily mean that they are implemented identically, but I would personally be surprised if there was a difference in a decent compiler.

Possible to expose CONSTANTS defined in webservice to consuming client (.Net both sides)

I have several constant defined for a webservice, and I would like to make these available to consuming client. Both the server and client are .Net.

I think it is possible to achieve this by using enum's, but many of my constants are text string with spaces, so I would have to write an extra function to convert the enum into the equivalent text string.

So, is there any way of defining the constants in the web service so they would then be available to the client??

Update (Kev): I want to expose them to the client via the web service itself, not via a separate assembly. Update #2 (Paige): So if I understand you, I will then have a new List object containing the constants, but how does the client use that? Wouldn't it look like (roughly): dim constants as List = mywebservice.GetConstants() dim someresult as Integer = mywebservice.somefunction(constants(3)) Unless I misunderstand you, that totally defeats the point of defining constants.

From stackoverflow
  • You could implement these constants in a standalone assembly and reference the assembly from both the web service and the client.

    Kev : Um...why the downvote?
    tbone : IMO, for "violation" of the spirit of the question. Of course I know you can distribute a seperate asssembly, I am trying to expose them via the webservice itself. And you knew this didn't you?
    Kev : You did say that both client and server are both .NET so the assumption being you have control over both client and server. Hence the suggestion.
    Kev : But at least you came back and commented/explained, so I'll take it on the chin like a man. :)
    tbone : Haha, good. I don't think this problem is solveable the way I want to. It can be done with enumerations, but not constants it doesn't seem.
  • You could write the "constants" into a dictionary and then have a web method that returns the keys.

    Or, using Kev's answer above:

    Class:

    public class Win32Message   {
        public const int WM_ACTIVATE =0x0006;
        public const int WM_ACTIVATEAPP = 0x001C;
        public const int WM_AFXFIRST = 0x0360;
        public const int WM_AFXLAST = 0x037F;
        public const int WM_APP = 0x8000;
        public const int WM_ASKCBFORMATNAME        = 0x030C;
    }
    

    And then in the web service use a web method like:

    [WebMethod]
    public System.Collections.Generic.List<string> GetConstants(System.Type type)
    {
        System.Collections.Generic.List<string> constants = new 
            System.Collections.Generic.List<string>();
    
        System.Reflection.FieldInfo[] fieldInfos = type.GetFields(
            System.Reflection.BindingFlags.Public | BindingFlags.Static |
            System.Reflection.BindingFlags.FlattenHierarchy);
    
        // Go through the list and only pick out the constants
        foreach (System.Reflection.FieldInfo fi in fieldInfos)
        if (fi.IsLiteral && !fi.IsInitOnly)
            constants.Add(fi.Name);
    
        return constants;
    }
    

    I found this on Wes' Puzzling Blog

    Of course, you can return it as an array, arraylist or however you'd like.

    Kev : Linky to Wes's article is broken...had a look on his site for it but couldn't spot.
    tbone : So if I understand you, I will then have a new List object *containing* the constants, but how does the client use that? Wouldn't it look like (roughly): dim constants as List = mywebservice.GetConstants() dim someresult as Integer = mywebservice.somefunction(constants(3)) Not very useful.
    tbone : Wes's article.... http://weblogs.asp.net/whaggard/archive/2003/02/20/2708.aspx
  • I don't know that there's any way to expose constants per se.

    But perhaps you could just implement some functions which always return the same value and just give them a special naming convention? For example:

    public int FLAG_READONLY()
    {
       return 3;
    }
    

    I may be misunderstanding your need.

  • As i see it, the real point of webservices is being able to retrieve data or execute processes on a server in a LANGUAGE and MACHINE-independent way, using xml or json or whatever as the language for data representation. When you call a service to list products, you just want to get the products, you don't want (or need) to know if you're calling a stored SQL procedure or a C# business function in the server. So i don't think it makes sense to share 'constants' from the client to the server unless you have a web method that lists those constants to you (again, in a text-only way). I think the whole Microsoft web service .asmx stack just sucks because it relies on the 'remote procedure call' metaphor instead of the 'service' part of the web service concept.

    I agree this is subjective :P

    John Saunders : You're wrong about the ASMX stack relying on RPC. It handles "document" style web services just fine.
  • The answer seems be.....cannot be done.

Raising a decimal to a power of decimal ?

The .net framework provides in the Math class a method for powering double. But by precision requirement I need to raise a decimal to a decimal power [ Pow(decimal a, decimal b) ]. Does the framework have such a function? Does anyone know of a library with this kind of function?

From stackoverflow
  • Are you sure you actually want to do this? A decimal multiply is about 40 times slower than double's, so I'd expect a decimal Math.Pow() to be practically unusable.

    If you expect only integer powers, though, I suggest you use the integer-based power algorithm that was already discussed here on SO.

    duffymo : "unusable"? Decimals raised to decimal powers are common in scientific computing (e.g., relationships between Nusselt, Reynolds, and Prandtl numbers in fluid mechanics). I doubt that it'll be a problem.
    Christoph Rüegg : Hardly - that would be the first time I've heard of anyone using a base-10 type like System.Decimal for scientific computations. Especially not in any area related to physics, like fluid mechanics. What is so special about these relationships that requires a base-10 type?
    P Daddy : @Christoph: I'd point out that most calculators (hardware and software) use base-10 computation.
    P Daddy : @duffymo: I, too, would expect a Pow(decimal, decimal) function to perform a few orders of magnitude slower than Pow(double, double), owing largely to the fact that the FPU cannot be utilized for decimals...
    P Daddy : ... Whether or not this would classify it as "unusable" depends on how many such calculations one expected to do in a given amount of time.
  • I think it depends a lot on the number you plan on plugging in. If 'a' and 'b' are not 'nice' number then you'll likely get a value which is non-terminating that is impossible to store and if C# BigDecimal behaves at all like Java BigDecimal it probably throws an exception in such a case.

  • log(c^d) = d * log(c)

    further...

    c^d = antilogarithm(d * log(c))

    Code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("169^(1/2) = " + RaiseToPower((169), (.5)));
                Console.ReadLine();
            }
    
            public static double RaiseToPower(double a, double b)
            {
                return Math.Exp(b * Math.Log(a));
            }
        }
    }
    
    Christoph Rüegg : The framework provides neither Log/Ln nor Exp for System.Decimal (remember, decimal is NOT intended for scientific computations).
    Chris Ballance : The question asked how to raise a decimal to a decimal power. No one mentioned a need for floating-point precision
    P Daddy : The question specifically mentions "Pow(decimal a, decimal b)".
    norheim.se : In what way would you say this solution is better than just using the framework's Math.Pow function? Both take doubles as input and return doubles. Neither handles System.Decimal.
  • To solve my problem I found some expansion series, and them I had them implemented to solve the equation X^n = e^(n * ln x).

      // power series
      int iteration = 27; // Adjust this to modify the precision
      decimal result = 1; 
      while (iteration > 0)
      {
        fatorial = Factorial(iteration);
        result += (Pow(power, iteration) / fatorial);
        iteration--;
      }
    
      // natural logarithm series
      decimal aux = (value - 1);
      decimal result = 0;
      while (iteration > 0)
      {
        result += Pow(aux, iteration) / iteration;
        iteration--;
      }
    

    The Pow() and Factorial() functions are simple because the power is always an int (inside de power series).

How can I increment a date by one day in Java?

I am getting date in the format as yyyy-mm-dd. I need to increment this by one day. How can I do this?

From stackoverflow
  • Convert it to a date, add one day and convert it back to the string with the specific format.

    Mark Robinson : How would add one day to a java.sql.Date object? The only way I can see is to add milliseconds but you running in to issues with day light savings this way.
    Mehrdad Afshari : See other answers. I prefer not to give "perfect code snippets" to homework style questions.
    Mark Robinson : Other answers use Calendar object, I ask b/c i've used the Date object in the past and ran into the day light saving problem. Just curious to see if there was another way to do it that i didn't think of.
    Mehrdad Afshari : Mark: I would have done it with the calendar object. Not sure if there is another good way.
    Mark Robinson : Ok thanks Mehrdad just wondering.
  • can u pleae give me example to do that afshari

    Geoffrey Chetwood : This is not an answer, please put this in a comment or edit your question.
    Michael Myers : A reasonable request, but not an answer. Could you please move this to a comment on his answer instead?
    krosenvold : You should either update your question or add a comment to the answer in question. Do not add a further question as an answer.
  • Construct a Calendar object and use the method add(Calendar.DATE, 1);

  • Use the DateFormat API to convert the String into a Date object, then use the Calendar API to add one day. Let me know if you want specific code examples, and I can update my answer.

  • SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
    Calendar cal = Calendar.getInstance();
    cal.setTime( dateFormat.parse( inputString ) );
    cal.add( Calendar.DATE, 1 );
    
    MetroidFan2002 : Downvoted: This answer assumes Calendar is a GregorianCalendar, which ignores the current Locale. Use Calendar.getInstance() instead.
  • Something like this should do the trick:

    String dt = "2008-01-01";  // Start date
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c = Calendar.getInstance();
    c.setTime(sdf.parse(dt));
    c.add(Calendar.DATE, 1);  // number of days to add
    dt = sdf.format(c.getTime());  // dt is now the new date
    
    Esko : c.roll(Calendar.DATE, true); would be somewhat better for clarity.
    Sam Hasler : @Esko, c.roll(Calendar.DATE, true) won't roll the month on the last day of the month.
  • Take a look at Joda-Time (http://joda-time.sourceforge.net/).

    DateTimeFormatter parser = ISODateTimeFormat.date();
    
    DateTime date = parser.parseDateTime(dateString);
    
    String nextDay = parser.print(date.plusDays(1));
    
    MetroidFan2002 : You can remove the parser calls for constructing the DateTime. Use DateTime date = new DateTime(dateString); Then, nextDay is ISODateTimeFormat.date().print(date.plusDays(1)); See http://joda-time.sourceforge.net/api-release/org/joda/time/DateTime.html#DateTime(java.lang.Object) for more info.
  • Date d1 = new Date();

    Date d2 = new Date();

    d2.setTime(d1.getTime() + 1 * 24 * 60 * 60 * 1000);

Testing Outlook VSTO Addins

I am trying to write a really simple Outlook VSTO add in that checks email that is being sent for a few simple properties. My problem is that I cannot seem to install/test the add in using Outlook. I have added unit tests that ensure the code I have written likely does what it should but that final level of integration eludes me. Any suggestions for how to test my code within Outlook? Thanks in advance.

If you want I can post the code as well and better explain what it does.

From stackoverflow
  • If you are coding in Visual Studio debugging automatically places it in Outlook and Opens Outlook. Just make sure Outlook is closed before starting as this seems to cause issues. Otherwise in your bin folder where your dll was created there is a vsto file, Click on that and it will install it.

    smaclell : Thanks for your help I will give it a shot. I tried using the VSTO file and that appeared to fail. I also tried F5 debugging but I probably had outlook open at the time. Will let you know how it goes.
    Alex : No problem, hope it helps. you got lucky because I just started working on vsto this week so I had to figure this stuff out recently ;)
    smaclell : F5 debugging worked like a charm when outlook was not already started. I have been having alot of issues with the vsto as I think I installed a bad version of the addin and now cannot get rid of it. However, that is not a programming question. Thanks you for your help.

Random banner only displaying the first image.

I have an external javascript that contains:

function random_imglink(){  
var myimages=new Array()  
myimages[1]="http://sevir.sitegoz.com/jchs/Banner1.png"
myimages[2]="http://sevir.sitegoz.com/jchs/banner2.png"
myimages[3]="http://sevir.sitegoz.com/jchs/banner3.png"
myimages[4]="http://sevir.sitegoz.com/jchs/banner4.png"
myimages[5]="http://sevir.sitegoz.com/jchs/banner5.png"
myimages[6]="http://sevir.sitegoz.com/jchs/banner6.png"

var ry=Math.floor(Math.random()*myimages.length)
if (ry==0)
ry=1
document.write('<img src="'+myimages[ry]+'" border=0>')
}

And my html contains:

<script language="JavaScript">
<!--
random_imglink()
//-->
</script>

But only the first image loads.
My site is here, the banner is supposed to change every time any of the pages loads (or reloads) but only the first image shows up, any ideas?

From stackoverflow
  • Apache servers are case sensitive. You have "Banner1" capitalized, but none of the other banners. When I try to navigate to the other images (with a lower case 'b'), I am redirected to the hosting site - telling me the file names do not exist.

    This may not be why you aren't getting the other images, but it certainly isn't helping.

    Re0sless : looks like this is the problem as the img url changes every time I try and load the page, but the image is missing, capitalising the B make the url work ok
    Sevir : I didn't even see that, thanks bud