keyword-argument

Pass username to Django form

Pass username to Django form Question: I have a custom Form where I need to pass in the request.user.username so I can use that to fill out forms.ModelChoiceField based on what user is accessing it. Here is what I have views.py if request.method == ‘POST’: form = DepartmentForm(request.POST, username=request.user.username) # Do stuff else: form = …

Total answers: 2

How to make "keyword-only" fields with dataclasses?

How to make "keyword-only" fields with dataclasses? Question: Since 3.0 there is support to make an argument keyword only: class S3Obj: def __init__(self, bucket, key, *, storage_class=’Standard’): self.bucket = bucket self.key = key self.storage_class = storage_class How to get that kind of signature using dataclasses? Something like this, but preferably without the SyntaxError: @dataclass class …

Total answers: 2

Is IntelliJ Python 3 inspection "Expected a dictionary, got a dict" a false positive for super with **kwargs?

Is IntelliJ Python 3 inspection "Expected a dictionary, got a dict" a false positive for super with **kwargs? Question: I use Python 3 and want to wrap argparse.ArgumentParser with a custom class that sets formatter_class=argparse.RawDescriptionHelpFormatter by default. I can do this successfully, however IntelliJ IDEA 2017.1 with Python Plugin (PyCharm) gives a warning for the …

Total answers: 3

Call a python function with named parameter using python.net from a C# code

Call a python function with named parameter using python.net from a C# code Question: I want to call a python function from C# code. To do that, I used Python for .NET to call function as shown in the following lines of code using System; using Python.Runtime; public class Test{ public static void Main(){ using(Py.GIL()){ …

Total answers: 2

Python: How to increase/reduce the fontsize of x and y tick labels?

How to increase/reduce the fontsize of x and y tick labels Question: I seem to have a problem in figuring out how to increase or decrease the fontsize of both the x and y tick labels while using matplotlib. I am aware that there is the set_xticklabels(labels, fontdict=None, minor=False, **kwargs) function, but I failed to …

Total answers: 4

Pass keyword arguments to target function in Python threading.Thread

Pass keyword arguments to target function in Python threading.Thread Question: I want to pass named arguments to the target function, while creating a Thread object. Following is the code that I have written: import threading def f(x=None, y=None): print x,y t = threading.Thread(target=f, args=(x=1,y=2,)) t.start() I get a syntax error for “x=1”, in Line 6. …

Total answers: 3

Add a parameter into kwargs during function call?

Add a parameter into kwargs during function call? Question: Is there a way to add key-value-pair into kwargs during the function call? def f(**kwargs): print(kwargs) # … pre_defined_kwargs = {‘a’: 1, ‘b’: 2} f(**pre_defined_kwargs, c=3) Or even change the existing arguments? f(**pre_defined_kwargs, b=3) # replaces the earlier b=2 These two examples don’t work, as they …

Total answers: 2

Using an OrderedDict in **kwargs

Using an OrderedDict in **kwargs Question: Is it possible to pass an OrderedDict instance to a function which uses the **kwargs syntax and retain the ordering? What I’d like to do is : def I_crave_order(**kwargs): for k, v in kwargs.items(): print k, v example = OrderedDict([(‘first’, 1), (‘second’, 2), (‘third’, -1)]) I_crave_order(**example) >> first 1 …

Total answers: 3

Python: Passing parameters by name along with kwargs

Python: Passing parameters by name along with kwargs Question: In python we can do this: def myFun1(one = ‘1’, two = ‘2’): … Then we can call the function and pass the arguments by their name: myFun1(two = ‘two’, one = ‘one’) Also, we can do this: def myFun2(**kwargs): print kwargs.get(‘one’, ‘nothing here’) myFun2(one=’one’) So …

Total answers: 3

How To Check If A Key in **kwargs Exists?

How To Check If A Key in **kwargs Exists? Question: Python 3.2.3. There were some ideas listed here, which work on regular var’s, but it seems **kwargs play by different rules… so why doesn’t this work and how can I check to see if a key in **kwargs exists? if kwargs[‘errormessage’]: print(“It exists”) I also …

Total answers: 7