class-attributes

Why can't class attributes be named as reserved words in python?

允我心安 提交于 2020-02-03 11:01:29
问题 It seems reserved words can not be used as attributes in python: $ python Python 3.6.2 |Continuum Analytics, Inc.| (default, Jul 20 2017, 13:51:32) [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> class A: >>> global = 3 File "<stdin>", line 2 global = 3 ^ SyntaxError: invalid syntax This seems sensible, since it is ambiguous: am I using the global keyword here? Difficult to say. But this is not sensible imho: >>> class

Lazy class property decorator

蹲街弑〆低调 提交于 2020-01-30 06:38:45
问题 I have one django model which needs to do some processing referring the custom user model. I can't work with the class of this model at class loading time because the loading order of the classes is unknown. So I need to add some class attributes at runtime, at the moment I'm adding them in the __init__ or __new__ like: def __new__(cls, *args, **kwargs): # hack to avoid INSTALLED_APPS initialization conflicts. # get_user_model() can't be called from this module at class loading time, # so

load_resource function not found as a class method of FPDF

妖精的绣舞 提交于 2020-01-15 09:16:06
问题 I am looking at the answer to the following question: Insert Base64 image to pdf using pyfpdf The answer suggested here was to override the existing load_resource method. What I did instead was class EnhancedPdf(FPDF): def load_resource(self, reason, filename): if reason == "image": if filename.startswith("data"): f = filename.split("base64,")[1] f = base64.b64decode(f) f = BytesIO(f) return f else: return super().load_resource(reason, filename) However, Pycharm highlights the super call with

How to teach SpecFlow to add additional NUnit attributes to my test class

南笙酒味 提交于 2020-01-12 07:45:08
问题 SpecFlow is great - and it helps us very much to do proper integration testing. One thing I was wondering is whether there's a way to tell SpecFlow to add additional NUnit attributes to the test class it creates in the feature code-behind file. Right now, my test class gets generated something like this: [System.CodeDom.Compiler.GeneratedCodeAttribute("TechTalk.SpecFlow", "1.8.1.0")] [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] [NUnit.Framework.TestFixtureAttribute()] [NUnit

Decorated class looses acces to its attributes

孤人 提交于 2019-12-25 05:35:24
问题 I implemented a decorator that worked like a charm until I added attributes to the decorated class. When I instantiate the class, it cannot acces the calss attributes. Take the following minimal working example : from module import specialfunction class NumericalMathFunctionDecorator: def __init__(self, enableCache=True): self.enableCache = enableCache def __call__(self, wrapper): def numericalmathfunction(*args, **kwargs): func = specialfunction(wrapper(*args, **kwargs)) """ Do some setup to

Can I mark a class as not my code so the debugger steps over it?

痞子三分冷 提交于 2019-12-21 07:23:12
问题 I have a utility class that has been thoroughly tested, and I do not want the VS debugger to step into any of its methods. I think I have heard of a way to mark something as not my code so that the Just My Code debugger setting causes the debugger to step over these method calls, but for the life of me I cannot recall what the class attribute is (nor can I successfully Google for it). I know that I could separate this class into its own assembly and build it in release mode to alleviate the

Dictionary class attribute that refers to other class attributes in the definition

南楼画角 提交于 2019-12-20 03:14:49
问题 While there are numerous ways around this, because of a personality fault I can't let it go until I understand the nature of the failure. Attempting: class OurFavAnimals(object): FAVE = 'THATS ONE OF OUR FAVORITES' NOTFAVE = 'NAH WE DONT CARE FOR THAT ONE' UNKNOWN = 'WHAT?' FAVES = defaultdict(lambda: UNKNOWN, {x:FAVE for x in ['dog', 'cat']}) FAVES['Crab'] = NOTFAVE Fails with: 3 NOTFAVE = 'NAH WE DONT CARE FOR THAT ONE' 4 UNKNOWN = 'WHAT?' ----> 5 FAVES = defaultdict(lambda: UNKNOWN, {x

React Js conditionally applying class attributes

笑着哭i 提交于 2019-12-17 02:56:06
问题 I want to conditionally show and hide this button group depending on what is passed in from the parent component which looks like this: <TopicNav showBulkActions={this.__hasMultipleSelected} /> .... __hasMultipleSelected: function() { return false; //return true or false depending on data } .... var TopicNav = React.createClass({ render: function() { return ( <div className="row"> <div className="col-lg-6"> <div className="btn-group pull-right {this.props.showBulkActions ? 'show' : 'hidden'}"

python class attribute cannot used as an argument for constructor?

◇◆丶佛笑我妖孽 提交于 2019-12-11 17:48:46
问题 In python 3 I found that class attribute can be used as a argument in __init__() function, like below: file test.py: class Foo: var1 = 23333 def __init__(self, var=var1): self.var = var run in cmd: C:\Users\rikka\Desktop>py -3 -i test.py >>> f1=Foo() >>> f1.var 23333 but by using a dot.expression, when init this class, interpreter will report an error: file test2.py: class Foo: var1 = 23333 def __init__(self, var=Foo.var1): self.var = var run in cmd: C:\Users\rikka\Desktop>py -3 -i test2.py

Comments within style= attributes - safe?

别来无恙 提交于 2019-12-11 10:21:39
问题 I am working on a CMS that generates CSS "style='xyz'" statements from user input. The user input will be validated but as an additional safeguard, I want to check the validity of the values on generation of the CSS code. If an invalid value is encountered - e.g. a relative width ("50%") where only absolute values are allowed due to layout restrictions - I would like to return a comment INSIDE the style attribute to help debugging: <div class="content" style="background-color: lightblue; /*