patching a class yields “AttributeError: Mock object has no attribute” when accessing instance attributes

No, autospeccing cannot mock out attributes set in the __init__ method of the original class (or in any other method). It can only mock out static attributes, everything that can be found on the class. Otherwise, the mock would have to create an instance of the class you tried to replace with a mock in … Read more

Safari ignores mp4 files

I found that the following accept string will add mp4 and m4v to the list of file types that safari will accept: accept=”video/mp4,video/x-m4v,video/*” I’m not sure what the mime type is for webm videos but if you can look that up you should be able to tack it on to the accept string. The trick … Read more

How to get a Token from a Lucene TokenStream?

Yeah, it’s a little convoluted (compared to the good ol’ way), but this should do it: TokenStream tokenStream = analyzer.tokenStream(fieldName, reader); OffsetAttribute offsetAttribute = tokenStream.getAttribute(OffsetAttribute.class); TermAttribute termAttribute = tokenStream.getAttribute(TermAttribute.class); while (tokenStream.incrementToken()) { int startOffset = offsetAttribute.startOffset(); int endOffset = offsetAttribute.endOffset(); String term = termAttribute.term(); } Edit: The new way According to Donotello, TermAttribute has been … Read more

What’s the difference between using the Serializable attribute & implementing ISerializable?

When you use the SerializableAttribute attribute you are putting an attribute on a field at compile-time in such a way that when at run-time, the serializing facilities will know what to serialize based on the attributes by performing reflection on the class/module/assembly type. [Serializable] public class MyFoo { … } The above indicates that the … Read more

InternalsVisibleTo attribute isn’t working

Are you absolutely sure you have the correct public key specified in the attribute? Note that you need to specify the full public key, not just the public key token. It looks something like: [assembly: InternalsVisibleTo(“MyFriendAssembly, PublicKey=0024000004800000940000000602000000240000525341310004000001000100F73 F4DDC11F0CA6209BC63EFCBBAC3DACB04B612E04FA07F01D919FB5A1579D20283DC12901C8B66 A08FB8A9CB6A5E81989007B3AA43CD7442BED6D21F4D33FB590A46420FB75265C889D536A9519 674440C3C2FB06C5924360243CACD4B641BE574C31A434CE845323395842FAAF106B234C2C140 6E2F553073FF557D2DB6C5”)] It’s 320 or so hex digits. Not sure why you need to specify the … Read more

Read the value of an attribute of a method

You need to call the GetCustomAttributes function on a MethodBase object. The simplest way to get the MethodBase object is to call MethodBase.GetCurrentMethod. (Note that you should add [MethodImpl(MethodImplOptions.NoInlining)]) For example: MethodBase method = MethodBase.GetCurrentMethod(); MyAttribute attr = (MyAttribute)method.GetCustomAttributes(typeof(MyAttribute), true)[0] ; string value = attr.Value; //Assumes that MyAttribute has a property called Value You can … Read more

Python string to attribute

Use the builtin function getattr. getattr(object, name[, default]) Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example, getattr(x, ‘foobar’) is equivalent to x.foobar. If the named attribute does … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)