Monday, February 21, 2011

Getting the pattern back from a compiled re?

Hi all. Assume I have created a compiled re:

x = re.compile('^\d+$')

Is there a way to extract the pattern string (^\d+$) back from the x?

From stackoverflow
  • You can get it back with

    x.pattern
    

    from the Python Regular Expression Objects page

    Bartosz Radaczyński : bill, do you know why dir(x) does not show pattern in the list?
    Bill the Lizard : I'm not sure. It could be because it's not a method, but a property.
    Bartosz Radaczyński : I'll post another question :)
    Bill the Lizard : Good idea, now I'm interested to know. :)
    Bartosz Radaczyński : I actually rephrased it a bit, since the docs say that the list need not to be complete (whatever Guido means by this statement...). check it out here: http://stackoverflow.com/questions/191010/how-to-get-a-complete-list-of-objects-methods-and-attributes

Saturday, February 19, 2011

Code is HttpClient or servlet API to parse Cookie header

Is there any existing code in Apache HttpClient or in the servlet API to parse Cookie header and obtain from a string that contains "name1=value1; name2=value2; ..." a list of Cookie? Writing code to parse this doesn't seem too hard, but if there is already some existing code, I'd like to use it.

From stackoverflow
  • If you call getCookies() on the HttpServletRequest object, it will return an array of Cookie objects. If you need to frequently look up cookies by name, then it may be easier to put them in to a Map so it's easy to look them up (rather than iterate over the Array each time). Something like this:

    public static Map<String,Cookie> getCookieMap(HttpServletRequest request) {
     Cookie[] cookies = request.getCookies();
     HashMap<String,Cookie> cookieMap = new HashMap<String,Cookie>();
     if (cookies != null) {
      for (Cookie cookie : cookies) {
       cookieMap.put(cookie.getName(), cookie);
      }
     }
     return cookieMap;
    }
    

    If you're using HttpClient and not servlets, you can get the Cookie array using:

    client.getState().getCookies()
    

    where client is your HttpClient object.

Double value returns 0.

Here's an example:

Double d = (1/3);
System.out.println(d);

This returns 0, not 0.33333... as it should.

Does anyone know?

From stackoverflow
  • That's because 1 and 3 are treated as integers when you don't specify otherwise, so 1/3 evaluates to the integer 0 which is then cast to the double 0. To fix it, try (1.0/3), or maybe 1D/3 to explicitly state that you're dealing with double values.

  • Wow, thank you!

    But how about if i have:

    double d = (height/imageHeight)*imageWidth;

    What would I use on that? Double.valueOf() or something else?

    tvanfosson : Then just use a simple cast of one of the variables in the division: double d = ((double)height/imageHeight)*imageWidth;
    chriscena : Please add additional comments and questions as comments to the related answer and not as a separate answer to you question.
    recursive : Try double d = height*imageWidth/imageHeight;
  • If you have ints that you want to divide using floating-point division, you'll have to cast the int to a double:

    double d = (double)intValue1 / (double)intValue2
    

    (Actually, only casting intValue2 should be enough to have the intValue1 be casted to double automatically, I believe.)

  • And thank you too! Problem solved :)

  • Use double and not Double unless you need to use these values in the object sense. Be aware about the Autoboxing concepts

Hook to WM6 sound

How can i hook to the window mobile sound (driver?) and read the data while it is passing it to the speaker.

From stackoverflow

boost spirit extracting first word and store it in a vector

Hi,

I have problems with Boost.Spirit parsing a string.

The string looks like

name1 has this and that.\n 
name 2 has this and that.\n 
na me has this and that.\n

and I have to extract the names. The text "has this and that" is always the same but the name can consist of spaces therefore I can't use graph_p.

1) How do I parse such a string?

Since the string has several lines of that format I have to store the names in a vector.

I used something like

std::string name;
rule<> r = *graph_p[append(name)];

for saving one name but

2) what's the best way to save several names in a vector?

Thanks in advance

Konrad

From stackoverflow
  • I presume there is a reason why you are using Boost.Spirit and not STL's string's find method? E.g:

    string s = "na me has this and that.\n";
    myVector . push_back( s.substr( 0, s.find( "has this and that" ) ) );
    
  • I think this will do the trick:

    vector<string> names;
    string name;
    parse(str,
        *(  
           (*(anychar_p - "has this and that.")) [assign_a(name)]
           >> "has this and that.\n") [push_back_a(names, name)]
         ))
    
    Benoît : +1. Clean and simple !
  • vector<string> names;
    parse(str,
        *(  
           (*(anychar_p - "has this and that.")) [push_back_a(names)]
           >> "has this and that.\n")
         ))
    

    Hi,

    thanks for your response. I have some problems with this version because there are following lines which have a completely different format and they are also parsed as true because there obviously is no "has this and that.". So I changed it to

    vector<string> names;
    parse(str,
        *(  
           (*(anychar_p - "has this and that.") >> "has this and that.\n"))[push_back_a(names)]      
         ))
    

    Now it parses the lines right but I got a new problem with the push_back actor because now it is pushing the complete line " has this and that" into the vector. Is it possible to remove the "has this and that. " before pushing it back or do I have to manually edit the vector afterwards?

Opening separate windows with Start

Hello,

I am tring to open several instance of IE with the start command in a batch file. For example I want to open www.google.com and www.yahoo.com at the same time in separate windows.

Any help would be appreciated.

Regards, Aaron

From stackoverflow
  • $>start iexplore http://google.com
    $>start iexplore http://yahoo.com
    

How do you change a connection string dynamically in an object datasource in asp.net?

how to change connection string dynamically in object datasource in asp.net ?

From stackoverflow
  • protected void ObjectDataSource1_ObjectCreated(object sender, ObjectDataSourceEventArgs e)
    {
        if (e.ObjectInstance != null)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = MyConnectionManager.ConnectionString;
            e.ObjectInstance.GetType().GetProperty("Connection").SetValue(e.ObjectInstance, conn, null);
        }
    }
    

    I hope it helps.

  • I didn't get the above to work but this did:

      if (e.ObjectInstance != null)
      {
        ((ReportPrototype.ReleasedRatingsDataTableAdapters.RatingsViewTableAdapter)e.ObjectInstance).Connection.ConnectionString = ConfigurationManager.ConnectionStrings["RADSDataConnectionString"].ConnectionString;
      }
    

    ObjectInstance is the table adapter which in my case was the type bound to the ObjectDataSource.