Tag Info

Hot answers tagged

15

The reason you have two different methods is that they do two different things. The .equals method returns a boolean value indicating whether the object on which you call the method is equal to the object passed in as a parameter (for some definition of "is equal to" that is consistent with the nature of the object being compared). The .compareTo method ...


7

Forget the outer loop around for a moment, and think about the meaning of your method second_small, the abstraction it represents (I assume in your real code, you picked a more meaningful name). Then what you do in that method should depend on what if j == 'something': ... means, in contrast to that abstraction. For example, if j == 'something' is a ...


5

I would like to be able to abstract each ticket developed by the team in a way that it can be enabled or disabled at runtime by using a distributed shared configuration system. Ideally yes, everything. I won't go down this path, but I think being able to toggle immediately any change, at runtime, is a very powerful feature. I am just wondering if ...


5

The tools you list have nothing to do with code reviews. They are there exactly to let the reviewers to focus on more useful subjects. Code reviews are for important things which cannot be automated: using a design pattern when another one matches better, over-documenting clear code or under-documenting unclear one, not using language features which can ...


5

This confusion would occur in situations where there are conflicting understandings of equals. compareTo is 'easy' in that it asks a simple question 'which one is bigger?' If you were given a bunch of Things, how do you sort them? equals on the other hand wants to ask 'are these the same thing?' BigDecimal in Java is one such place where there are ...


5

If func_2 in MyClass1 doesn't contain any logic and is expected to be declared by child classes (and MyClass1 is never used directly), then making the class abstract like you did is a reasonable approach and makes the code self-documenting and explicit. If: func_2 in MyClass1 contains logic (eventually overwritten in child classes), Or MyClass1 may be ...


4

Contractors from various fields typically bring their entire toolbox to a job. Sometimes, they even bring tools that they're not entirely familiar with, on the off chance that they might find them necessary... But also, probably because those tools came in a set, and that set was made in a 'one-size-fits-all' fashion, as it seems has been done here. You may ...


3

A dictionary comprehension is expressive in the sense that by using one, you are expressing that your intent is to create a dictionary. You can always do the same thing in a for loop, but doing so does not express that intent. It is something the reader has to determine by code inspection. So consider this: cache = {key:lookup(key) for key in keystore} ...


3

To me, "expressive" means that you can express a large diversity of concepts with it. In many programming languages, a foreach loop (iterating over a container) is less "expressive" than a for loop. Because you can always trivially rewrite the former in terms of the latter, but not vice versa. Analogously, I would say that dictionary comprehensions are not ...


3

First of all, once you start decomposing dates into their constituent components, they are no longer dates. In the same way that it is not possible to remove functionality via subclasses without breaking OOP, it is not possible to mix dates and date fractions without causing confusion (or worse) making them compatible as in your code example without ...


3

So you effectively need (or just want?) to have two source codes for the pre-ticket and post-ticket situation - e.g. ticket modified behavior of one method, so you ideally want to be able to switch from the former implementation (pre-ticket) of the method to the new (post-ticket) implementation. This can be done using polymorphism - you have some e.g. ...


3

Don't slice your list, pass start and end indices instead def Fun(I): def Inner(I, L, R): for i in range(L,R): if i==[i]: return i elif I[len(I)/2-1]>len(I)/2-1: return Inner(I, L, L + (R/2)) else: return Inner(I, L/2, R) return Inner(I, 0, len(I)-1)


2

Here's a starting point - a function that checks whether that constraint can be met somewhere within that range... def Can_Exist_In_Range (xs, lo, hi) : return not ((xs [hi] < lo) || (xs [lo] > hi)) This is using inclusive bounds for convenience, which is actually pretty bad practice in general. If the lowest value is higher than the high index ...


2

But are there legitimate cases of java/c#/python/etc Object.equals() method behaving inconsistently with the method Comparable.compareTo()? Yes, for example, here is a legitimate case in Python 2.x. The float value for NaN does not compare equal (==) to itself, yet cmp() (equivalent to compareTo) will return 0 (equal), which is inconsistent with the ...


2

The "legitimate reason" for having equals() inconsistent with compareTo() is if they serve different real-world purposes. Let's start with BigDecimal. If you're simply sorting a list of values, you probably don't care about scale. However, if you're checking for equality, what you're really checking is that the series of operations that led to this result ...


2

I don't know that there is one answer that everyone would agree with, but personally I would just use the built in Python classes. Reading JSON to python dictionaries and lists is a very standard thing to do, it maps very well. I'm all and for OO programming, but I don't see the point in writing a tiny class to be a thin wrapper over existing powerful data ...


2

Linux packages shipped with a given distribution are usually heavily tested to ensure they work well with the specific distribution. Your company may do an additional step of testing those packages for the scenarios which happen specifically in the company. If you're installing newer versions, you're on your own. It may work well, and it usually does, ...


2

What practices can I use to maintain the code clean and avoid series of if/else cluttering the code Though your original plan sounds a little bit crazy, if you replace the requirement of "every ticket" by "a lot of different features", then the above questions makes IMHO a lot more sense. To my experience, the key to keep code maintainable when having a ...


2

Robert Harvey's second comment contains the right answer, but let me expand on it a bit. People's year of birth and people's birthdates are completely different entities, so you do not need to (and actually you should not) use the same mechanism for both. For birthdates, you can devise a BirthDate data type (or possibly a YearlyRecurringDate though I ...


2

some options are: using immutable trees as basic data structure using a builder or mutation operations to construct a FooTree without needing a Tree copying the input tree (which is good practice unless you have really big data and performance issues) Note that by builder I mean the builder pattern as described by Joshua Bloch. (Long article, but the ...


1

IMO expressiveness is a trait of the language/framework/library as a whole - not of a single feature/function in it. Some operations can be better expressed with for loops, some with dictionary comprehensions - so I wouldn't say that one is more expressive than the other, but that Python as a whole is more expressive for having both. Compactness is ...


1

One option is to make it clear to the IDE and your users that there is a method, but you can't use the base class version: class MyClass1(object): def func_2(self): raise NotImplementedError def func_1(self): return self.func_2() * 2 You won't get warnings from intermediate classes that don't define func_2, but will get an error ...


1

Anaconda minimal install is about 30MB, while full install is about 300MB. so your customized libraries can range in that interval. The main question is if servers disk space is so critical that 200MB make big difference. In that case, you can always do a script to install 'minimal version' and your choosen libraries. If you are thinking on deploy on ...


1

I would use built-in lists for any lists (uniform variable-length sequences of objects). I would use namedtuple-derived classes to store data with known fixed sets of named fields. It's like objects, but immutable. So you'd have your text.tokens[3].part_of_speech == NOUN, but won't have text.tokens[3].part_of_speech = NOUN. Immutability is often useful at ...


1

Instead of keeping 3 member fields and synchronizing them, you only need to store one actual value in memory - the radius is a good choice in this example - and always use it: import cmath class circle(object): def __init__(self, radius=0.0): self._radius = radius @property def radius(self): return self._radius ...


1

We are using something very similar to this. Can't you add a task that saves the data once it has completed processing them? We use Kombu to download (Consumer) and to Publish (Producer) back to RMQ and celery only has a very specific task which sort of is your case. RMQ queue A gets consumed by Consumer Message obtained by Consumer gets sent to Celery ...


1

The problem you are asking about is a well known problem which has a plethora of applications: For example the task to minimize material waste in furniture production: Certain pieces (your list of rectangles) have to be cut out of boards of plywood in a given size (your bounding rectangle). As you seem to have already figured out finding all possible ...


1

Forget my earlier answer. I actually implemented it now and it works. Not spoiling the whole thing but just answering your question, here's part of my code: def Fun(I): lo, hi = 0, len(I) # We'll search I[lo:hi], i.e., excluding index hi while lo < hi: check middle index adjust lo and hi appropriately handle the the ...


1

Since you're splitting up the lists, changing the indices, you need to reflect that back into your return calls as the function calls unravel. Something like: def Fun(I): for i in range(0,len(I)-1): if i==[i]: return i elif I[len(I)/2-1]>len(I)/2-1: # no need to add if index is below return ...



Only top voted, non community-wiki answers of a minimum length are eligible