James Manning's Blog

January 14, 2012

Loading lambdas from a file

Filed under: Uncategorized — manningj @ 2:26 am

From this question on SO:

http://stackoverflow.com/questions/8857871/creating-lambda-expression-from-a-string

The below assumes you know the intended type of the lambdas you’re loading (you could make it more dynamic, but that didn’t seem likely in the target scenario to me).

It uses the existing  compiler within the framework, but since I don’t know of a way to compile at a more granular level (and still be string-based – so CodeDOM and the like don’t count Smile), this just compiles a single class with a single property into an assembly, then takes the assembly and pulls out the property.

With this for the input:

image

The output generated is:

image

https://gist.github.com/1610600

using System;
using System.Xml.Linq;
using System.Linq;
using System.IO;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Linq.Expressions;

class Program
{
    private const string classTemplate = @"
            using System;
            using System.Linq.Expressions;
    
            public static class RulesConfiguration
            {{
                private static Expression<Func<string, bool>>[] rules = new Expression<Func<string, bool>>[]
                {{
                    {0}
                }};
    
                public static Expression<Func<string, bool>>[] Rules {{ get {{ return rules; }} }}
            }}
        ";

    static void Main(string[] args)
    {
        var filePath = @"c:\temp\rules.txt";
        var fileContents = File.ReadAllLines(filePath);

        // add commas to the expressions so they can compile as part of the array
        var joined = String.Join("," + Environment.NewLine, fileContents);

        Console.WriteLine("Rules found in file: \n{0}", joined);

        var classSource = String.Format(classTemplate, joined);

        var assembly = CompileAssembly(classSource);

        var rules = GetExpressionsFromAssembly(assembly);

        foreach (var rule in rules)
        {
            var compiledToFunc = rule.Compile();
            Console.WriteLine("Checking rule {0} against input {1}: {2}", rule, filePath, compiledToFunc(filePath));
        }
    }

    static Expression<Func<string, bool>>[] GetExpressionsFromAssembly(Assembly assembly)
    {
        var type = assembly.GetTypes().Single();
        var property = type.GetProperties().Single();
        var propertyValue = property.GetValue(null, null);
        return propertyValue as Expression<Func<string, bool>>[];
    }

    static Assembly CompileAssembly(string source)
    {
        var compilerParameters = new CompilerParameters()
        {
            GenerateExecutable = false,
            GenerateInMemory = true,
            ReferencedAssemblies =
            {
                "System.Core.dll" // needed for linq + expressions to compile
            },
        };
        var compileProvider = new CSharpCodeProvider();
        var results = compileProvider.CompileAssemblyFromSource(compilerParameters, source);
        if (results.Errors.HasErrors)
        {
            Console.Error.WriteLine("{0} errors during compilation of rules", results.Errors.Count);
            foreach (CompilerError error in results.Errors)
            {
                Console.Error.WriteLine(error.ErrorText);
            }
            throw new InvalidOperationException("Broken rules configuration, please fix");
        }
        var assembly = results.CompiledAssembly;
        return assembly;
    }
}

January 11, 2012

That path sucks, try this one instead!

Filed under: Uncategorized — manningj @ 12:19 am

I should probably submit this to Alex @ TheDailyWTF, ya know…

 

image

January 7, 2012

It’s ‘lightning’, not ‘lightening’!

Filed under: Uncategorized — manningj @ 5:40 pm

image

 

I’m amazed (ok, not really) how often this gets messed up.  I see it a lot playing online when people decide to complain about use of the lightning gun, for instance.

Apparently Google (or, at least, their algorithms) have noticed as well!

image

github import from svn made me laugh

Filed under: Uncategorized — manningj @ 1:22 pm

image

November 4, 2011

SQL Server uses an odd number line

Filed under: Uncategorized — manningj @ 3:37 pm

While it’s likely just a hyphen that needs to be removed from the resource string, it makes for an odd error message as-is. Smile

C:\ » sqlcmd -h 0
Sqlcmd: ‘-h 0′: header value must be either -1 or a value between -1 and 2147483647

November 3, 2011

a company really sad to see you unsubscribe

Filed under: Uncategorized — manningj @ 10:11 am

Definitely an interesting experience as I unsubscribed from AppSumo (was just switching from email to RSS, FWIW)

image

November 2, 2011

OCZ, SandForce, and “no material impact”

Filed under: Uncategorized — manningj @ 3:04 pm

As November 1st has come and gone and OCZ is still silent on the Octane (the Oct 20th press release is the only meaningful hit on their site, the other ‘octane’ is talking about SGI), one wonders what’s going on over there.

Without any real knowledge myself, I’m left to speculation, but since it was just a week ago that the OCZ CEO decided to publicly claim that SandForce being acquired by LSI “will have no material impact to our existing product lines or business”, I wonder if that word ‘existing’ was added specifically because of the Octane since it’s pretty easy to argue that it wouldn’t fall under ‘existing’ a week ago since it hadn’t been made available to the public yet.  I don’t really know enough about SSD’s in general or OCZ’s in particular to know whether it’s even possible that the LSI acquisition could even have a material impact on the Octane or not. 

As OCZ’s twitter account remains silent and the OCZ stock on NASDAQ (at least the moment) remains relatively flat over this last week around $7/share, I wonder how many investors are getting awfully nervous based on this lack of communication from the company.

Certainly I would have been on the (I’d imagine very LONG) list of people asking for an Octane as a Christmas present, so perhaps it’s all just a mad scramble internally for them to figure out how they can still make it out in time for the holiday shopping season.

With Black Friday and Cyber Monday only 23 and 26 days away, respectively, whatever’s going on has got to be freaking some people out.  Hopefully it’ll at least be an interesting story once it all comes out!

I’m kind of surprised there’s nothing in the ‘trade mags’/blogs/whatever about this, but perhaps I’m just missing something and/or making a mountain out of a mole hill.  Either way, I’d love to find out WTF is going on with the Octane, sooner rather than later! Smile

November 1, 2011

Ok, it’s November 1st, where’s the Octane?

Filed under: Uncategorized — manningj @ 1:15 pm

So 12 days ago, OCZ announces their first Indilinx-based SSD since acquiring Indilinx earlier this year.

http://www.ocztechnology.com/aboutocz/press/2011/458

The OCZ Octane SSD Series will be available November 1st in models ranging from 128GB-1TB capacities throughout OCZ’s global channel.

 

The reaction of many, myself included, can best be represented by our dear friend SpongeBob:

image

 

Since today is The Day, I go to check OCZ’s 3 ‘premier resellers’, Amazon, newegg, and TigerDirect

The results?

Not on Amazon

Your search "ocz octane" did not match any products.

Not on newegg

We have found 0 items that match "ocz octane".

Not on TigerDirect

0 Results found for "ocz octane."

 

My reaction after these (and other) fruitless searches today?

image

October 14, 2011

Does TWC QoS down Earthlink traffic?

Filed under: Uncategorized — manningj @ 11:00 pm

Any ideas on how I could tell if I’m the only one in my area (this side of the second hop) seeing this, and if not, whether this is TWC just QoS’ing Earthlink traffic behind their own?

I wouldn’t want to switch from Earthlink to TWC cable if it’s not going to make a difference, but if it would, I’d happily do so. Smile

C:\Users\James>tracert 152.1.1.22

Tracing route to ns1.ncsu.edu [152.1.1.22]
over a maximum of 30 hops:

  1    <1 ms    <1 ms    <1 ms  DD-WRT [192.168.0.1]
  2   417 ms   397 ms   405 ms  user-0c8hjg1.cable.mindspring.com [24.136.206.1]
  3   447 ms   435 ms   459 ms  ten13-0-0-306.rlghnca-rtr2.nc.rr.com [66.26.44.117]
  4   235 ms   252 ms     *     ae19.chrlncpop-rtr1.southeast.rr.com [24.93.64.2]
  5   389 ms   399 ms   408 ms  ten1-3.chrlncsa-p-rtr01.southeast.rr.com [24.93.73.57]
  6   432 ms   450 ms   475 ms  por100.twcc.rlghnc-a-c2701.nc.rr.com [24.27.255.253]
  7   229 ms   237 ms   260 ms  rrcs-24-172-64-46.midsouth.biz.rr.com [24.172.64.46]
  8   294 ms   306 ms   306 ms  ncsu-gw-2-to-chlt7600-gw.ncren.net [128.109.248.62]
  9   373 ms   379 ms   367 ms  itcore-x-ncsugw2.ncstate.net [152.1.6.249]
10   435 ms   423 ms   459 ms  vl2910-dc2-6509-2.ncstate.net [152.1.6.226]
11   476 ms   481 ms   476 ms  ns1.ncsu.edu [152.1.1.22]

Trace complete.

October 13, 2011

after iOS 5 upgrade, media on iPhone all ‘gone’ and the space is now ‘Other’

Filed under: Uncategorized — manningj @ 10:57 pm

Apparently this has been happening for ages (at least going back to iTunes 7.x) and it appears to be related to sync operations not happening correctly/normally/whatever (although I didn’t perceive any sync problems on this particular phone).

image

As unexpected of a workaround as it is, I got lucky and ran across someone else that hit this today with their own iOS 5 upgrade.  Just drag a new song not currently on the device from the library onto the device, and sure enough, that did the trick.  The particular song synced over when I dropped it, and that triggered whatever necessary sanity check code that then resynced everything else on the device.  Yay!

http://gfmorris.com/2007/07/19/iphone-music-disappears-disk-space-shows-as-other/

image

Older Posts »

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.