How to Dry Up Method with Multiple { 'Not Found' }

How to DRY up method with multiple { 'not found' }?

I not sure about right syntax in product.price = product.deep_fetch(attribute, { 'not found' }):

mashie.products.each do |product|
product.extend Hashie::Extensions::DeepFetch

i%[name sale_price currency].each do |attribute|
product.price = product.deep_fetch attribute { 'not found' }
end

@products << product
end

How can I stop Kotlin creating multiple overloaded Java methods out of one Kotlin function with optional parameters

For such a function, if you don't use the @JvmOverloads annotation, Kotlin creates exactly two methods, regarding of the number of optional parameters. One method has the regular signature, and another one additionally accepts a bit mask of parameters that have been passed. There is no way to avoid creating multiple methods.

What I would do in this case is simply create two separate functions, "when trying to login or register without email" and "when trying to login or register with email address <email>".

How to start and stop multiple child processes from a class?

The root of the problem is that the start method of a multiprocessing.Process instance sets its _popen instance attribute to a multiprocessing.popen_*.Popen instance. The initialization of that instance performs these two steps (among others):

  1. For a multiprocessing.popen_spawn_posix.Popen instance, a multiprocessing.popen_spawn_win32.Popen instance, or a multiprocessing.popen_forkserver.Popen instance but not a multiprocessing.popen_fork.Popen instance (i.e. for the start method 'spawn' or the start method 'forkserver' but not the start method 'fork'), it serialises the multiprocessing.Process instance for writing it to the end of the pipe used by the parent process to communicate with the child process so that the child process can execute the run method of the multiprocessing.Process instance.

  2. It sets its finalizer instance attribute to a multiprocessing.util.Finalize instance which itself sets its _weakref instance attribute to a weakref.ref instance for closing at interpreter exit the ends of the pipes used by the parent process to communicate with the child process. In other words, it makes the multiprocessing.Process instance hold a weak reference.

Thus if a multiprocessing.Process instance holds a reference to a started multiprocessing.Process instance then it holds a weak reference (point 2), so starting it will fail since it will serialise (point 1) the weak reference and weak references are not serialisable:

import multiprocessing

if __name__ == '__main__':
multiprocessing.set_start_method('spawn') # or 'forkserver' but not 'fork'
process_a = multiprocessing.Process()
process_b = multiprocessing.Process()
process_b.foo = process_a
process_a.start() # creates process_a._popen.finalizer._weakref
process_b.start() # TypeError: cannot pickle 'weakref' object

A minimal Python program showing the serialisation issue:

import pickle
import weakref

pickle.dumps(weakref.ref(int)) # TypeError: cannot pickle 'weakref' object

Two workarounds that avoid the serialisation issue:

  • either make the target argument a classmethod so that it is not bound to self (and in particular to the instance attribute self._processes):
class Application:

def __init__(self):
self._event = multiprocessing.Event()
self._processes = [
multiprocessing.Process(target=self._worker, args=(self._event,))
for _ in range(multiprocessing.cpu_count())]

@classmethod
def _worker(self, event):
while not self._event.is_set():
print(multiprocessing.current_process().name)
time.sleep(1)

def start(self):
for process in self._processes:
print('starting')
process.start()

def stop(self):
self._event.set()
for process in self._processes:
process.join()
  • or exclude specifically the instance attribute self._processes from the serialisation of the target argument with __getstate__:
class Application:

def __init__(self):
self._event = multiprocessing.Event()
self._processes = [
multiprocessing.Process(target=self._worker)
for _ in range(multiprocessing.cpu_count())]

def _worker(self):
while not self._event.is_set():
print(multiprocessing.current_process().name)
time.sleep(1)

def start(self):
for process in self._processes:
print('starting')
process.start()

def stop(self):
self._event.set()
for process in self._processes:
process.join()

def __getstate__(self):
state = {}
for key, value in vars(self).items():
if key != '_processes':
state[key] = value
return state

How do I break out of nested loops in Java?

Like other answerers, I'd definitely prefer to put the loops in a different method, at which point you can just return to stop iterating completely. This answer just shows how the requirements in the question can be met.

You can use break with a label for the outer loop. For example:

public class Test {
public static void main(String[] args) {
outerloop:
for (int i=0; i < 5; i++) {
for (int j=0; j < 5; j++) {
if (i * j > 6) {
System.out.println("Breaking");
break outerloop;
}
System.out.println(i + " " + j);
}
}
System.out.println("Done");
}
}

This prints:

0 0
0 1
0 2
0 3
0 4
1 0
1 1
1 2
1 3
1 4
2 0
2 1
2 2
2 3
Breaking
Done

Numpy where with multiple conditions

You can use a ternary:

np.where(consumption_energy > 400, 'high', 
(np.where(consumption_energy < 200, 'low', 'medium')))

How to have multiple conditions for one if statement in python

I would use

def example(arg1, arg2, arg3):
if arg1 == 1 and arg2 == 2 and arg3 == 3:
print("Example Text")

The and operator is identical to the logic gate with the same name; it will return 1 if and only if all of the inputs are 1. You can also use or operator if you want that logic gate.

EDIT: Actually, the code provided in your post works fine with me. I don't see any problems with that. I think that this might be a problem with your Python, not the actual language.

C# Mutex how to stop my console app running multiple times

how to use the sample code.

take the sample code (its one file) and add it to your project (as a separate .cs file)

now at the startup of your program main add

   using(var spi = new SpecialServices.SingleProgramInstance("x5k6yz"))
{
if (!spi.IsSingleInstance){
Console.WriteLine("another copy is running");
return;
}
}

caveat, I have not tried the code from the sample, I assume it works.

EDIT. Ok tested, it works fine



Related Topics



Leave a reply



Submit