Tag Info

Hot answers tagged

9

A test like hassattr(tweet, 'setdefault') to make sure tweet is a dictionary is not a good one, since it obviously does not assure tweet provides all methods/properties of a dictionary. So as long tweet.setdefault is not the only method called by find_closest_state (which I think is unlikely), this test is not strict enough. On the other hand, a test like ...


8

Why cant be a global Standard to do all Simple and Common things? We tried that. The concept was called UNCOL, and the idea was that it would be ported to every architecture in the world, everyone would use it for everything. As you can tell, things didn't work out that way. Why not? Because programming is a complex activity, and there is no single ...


7

I generally avoid having the class know how to serialize itself, for a couple of reasons. First, if you want to (de)serialize to/from a different format, you now need to pollute the model with that extra logic. If the model is accessed via an interface, then you also pollute the contract. public class Image { public void toJPG(String filePath) { ... } ...


5

Let's immediately get the Turing-completeness disclaimer out of the way and say any language can probably approximate any runtime feature of any other language. Good? Good. The main difference between the Node.js approach and a Python threaded-server (or a typical Java HTTP server implementation) is that Node.js is single threaded while the latter two are ...


4

What matters about programming languages are the semantics, not the syntax. However, syntax is a vehicle for semantics. It is easy to show that two languages can have incompatible semantics (e.g. unrestricted pointers vs. memory safety, or differences in type systems). Let's focus on the syntax and semantics of variable declarations. When we declare a ...


4

First, I want to say that by far not everybody agrees that duck typing is a good thing at all, let alone that it is some sort of holy principle that should be followed. Often duck typing leads to error prone, hard to debug code, although it can be very flexible in how you can use it. Duck typing just takes an object and does with the object what it wants to ...


2

This is the job of the package manager. In the case of Python: pip. If you package the library you are building and specify its dependencies, your users (grandpa), will simply need to run: $ pip install --upgrade <name-of-package> to get the latest version you've released and all of its dependencies. If you don't want to publish your code online, ...


2

What you are looking for is called a REPL, a quick search for "C++ REPL" gets you to Cling, but I have not used it. I'm not sure what is the real benefit from using a REPL over to use a debugger with an interface that you are comfortable with. Static typing should help you get the code right before ever running it, also IDE's suggestions become much better ...


1

Serialization is a two part problem: Knowledge about how to instantiate a class aka structure. Knowledge about how to persist/transfer the information that is needed to instantiate a class aka mechanics. As far as possible, structure should be kept separate from the mechanics. This increases the modularity of your system. If you bury the information on ...


1

Generally speaking, you should not be modifying the source code files of third party code unless they're the type of changes for which it would make sense to contribute them back upstream (e.g. bug fixes). Obviously, there are exceptional situations where you may wish to go ahead and modify those files anyway, but you usually want to avoid forking the ...


1

You could create @property decorated functions. In the setter one, you make sure you have something that calls all the pre-defined callback for said property. This is a visitor pattern really. So, in untested and incomplete code, something akin to this: @ook.setter def ook(self, value): self._whatever = ook for func in self._ook_callbacks: ...


1

Use random.shuffle to rearrange the ints. E.g., http://stackoverflow.com/a/12978830/2877364. Then you can loop over the ints: random.shuffle(index) For I in index: # use dictionary[I]


1

First, if your library is generic, it should NOT be influenced by the application. It should provide the functionality that it is designed to provide. Application is based on the library, not the other way around. Whether you keep it the same repository or different one, doesn't matter - have it a separate project though. I would do a technical design ...


1

I'll start with the generic language neutral stuff first, and then I'll cover the python specific bits. Why are you so sure that there needs to be a seperate library? Is it because different developers working on the code? or are there plans to reuse the library in additional applications? or even to distribute the library on its own to customers. In ...


1

Here is the pythonic way to write that code: if val == 4: print("\nx is equal to the number 4") elif val == "elephant": print("\nx is equal to elephant") elif val == 14.38: print("\nx is equal to 14.38") elif val == "4": print("\nx is equal to the string 4") else: print("\nnope") It does exactly what your code does, but is more clear, ...


1

namedtuple has such a method because it itself is immutable. Other immutable types in the standard library have one too, like datetime.datetime for example. It is not a common pattern to use with mutable objects. So no, there is no built-in version for custom types for this. Custom classes invariably require custom handling anyway. Note that your utility ...


1

Below is an example of a test. BTW, you have a bug in your class. Probably self.i=self.i+i should be self.i=self.i+1 #!/usr/bin/env python # -*- Python -*- "Unit test for Columnize" import unittest from it import anything class TestI(unittest.TestCase): def test_basic(self): for x in anything(): if x % 2 == 0: ...



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