Constant DateTime in C#

As some of the earlier responses note, a const DateTime is not natively supported in C# and can’t be used as an attribute parameter. Nevertheless, a readonly DateTime (which is recommended over const in Effective C#, 2nd edition [Item 2]) is a simple workaround for other situations as follows: public class MyClass { public static … Read more

AllowAnonymous not working with Custom AuthorizationAttribute

In the AuthorizeAttribute there is the following code: private static bool SkipAuthorization(HttpActionContext actionContext) { Contract.Assert(actionContext != null); return actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any() || actionContext.ControllerContext.ControllerDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any(); } Include this method in your AuthorizeAttribute class then add the following to the top of your OnAuthorization method to skip authorization if any AllowAnonymous attributes are found: if (SkipAuthorization(actionContext)) return;

selecting attribute values from lxml

find and findall only implement a subset of XPath. Their presence is meant to provide compatibility with other ElementTree implementations (like ElementTree and cElementTree). The xpath method, in contrast, provides full access to XPath 1.0: print customer.xpath(‘./@NAME’)[0] However, you could instead use get: print customer.get(‘NAME’) or attrib: print customer.attrib[‘NAME’]

finding elements by attribute with lxml

You can use xpath, e.g. root.xpath(“//article[@type=”news”]”) This xpath expression will return a list of all <article/> elements with “type” attributes with value “news”. You can then iterate over it to do what you want, or pass it wherever. To get just the text content, you can extend the xpath like so: root = etree.fromstring(“”” <root> … Read more

getattr and setattr on nested subobjects / chained properties?

You could use functools.reduce: import functools def rsetattr(obj, attr, val): pre, _, post = attr.rpartition(‘.’) return setattr(rgetattr(obj, pre) if pre else obj, post, val) # using wonder’s beautiful simplification: https://stackoverflow.com/questions/31174295/getattr-and-setattr-on-nested-objects/31174427?noredirect=1#comment86638618_31174427 def rgetattr(obj, attr, *args): def _getattr(obj, attr): return getattr(obj, attr, *args) return functools.reduce(_getattr, [obj] + attr.split(‘.’)) rgetattr and rsetattr are drop-in replacements for getattr and … Read more

Custom Assembly Attributes

Yes you can. We do this kind of thing. [AttributeUsage(AttributeTargets.Assembly)] public class MyCustomAttribute : Attribute { string someText; public MyCustomAttribute() : this(string.Empty) {} public MyCustomAttribute(string txt) { someText = txt; } … } To read use this kind of linq stmt. var attributes = assembly .GetCustomAttributes(typeof(MyCustomAttribute), false) .Cast<MyCustomAttribute>();

Why can’t you add attributes to object in python? [duplicate]

Notice that an object instance has no __dict__ attribute: >>> dir(object()) [‘__class__’, ‘__delattr__’, ‘__doc__’, ‘__getattribute__’, ‘__hash__’, ‘__init__’, ‘__new__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__setattr__’, ‘__str__’] An example to illustrate this behavior in a derived class: >>> class Foo(object): … __slots__ = {} … >>> f = Foo() >>> f.bar = 42 Traceback (most recent call last): File … Read more

What’s the difference between the square bracket and dot notations in Python?

The dot operator is used for accessing attributes of any object. For example, a complex number >>> c = 3+4j has (among others) the two attributes real and imag: >>> c.real 3.0 >>> c.imag 4.0 As well as those, it has a method, conjugate(), which is also an attribute: >>> c.conjugate <built-in method conjugate of … Read more

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