Tag Info

New answers tagged

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 ...


0

A Node program is at its core a normal program, it isn't even a web-server unless you write code specifically to make it so (though that is pretty easy with the built-in HTTP library). Your program will get the HTTP requests, and your code can respond any way you like, using all the resources otherwise available to the program. If JavaScript was old-school ...


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 ...


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) { ... } ...


0

Unlike all other servers you have mentioned, Node is single threaded, but asynchronous - as some have mentioned here, it schedules callbacks instead of waiting for operation to execute and runs callbacks when the operation is complete, however a number of other operations might have been processed in between with the same thread. Caveats: Although it's a ...


0

You would probably be well-served looking through the numpy documentation, the library has a lot of optimised ways of doing simple and complex operations on ndarrays that avoid using multiple for-loops. You could just do this to achieve the same thing: import numpy as np total = np.sum(arraytransition) arraytransition_normalised = arraytransition / total ...


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 ...


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: ...


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, ...


0

Every programming language is invented by different mind set, experience and objective, and there is no universal guideline setup for creating a language. So whenever a new language comes up it's their inventor choice to think about best naming/ syntax so this is all up to them what they think is best will add there. For example to add two numbers i will ...


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 ...


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 ...


0

The basic stuff has to do with closing unneeded file handles, checking over signal handling and setting the proper user and group, as well as the daemonization process itself (attaching to init). On Linux systems or where possible in addition to setting uid and gid, there's setting a session id so that the process might be more easily found. There is a ...


0

Your teacher's definition completely misses the most important point of object-oriented programming, the one thing that makes it useful and unique. "Message passing" is a bunch of nonsense dreamed up by the Smalltalk folks, and it's been a failure everywhere it's been tried. The true power of OOP is something known as Liskov substitution, and while the ...


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 ...


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 ...


0

Here is one way to do it, which increments a collections.Counter every time a callable attribute is accessed: class LoggingMixIn: def __init__(self, *args, **kwargs): self._log = Counter() super().__init__(*args, **kwargs) def __getattribute__(self, name): attr = super().__getattribute__(name) if callable(attr): ...


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]


0

Unit tests give you value, and they create work. You want the best possible value for work. If you write ten tests each checking ten items, instead of 100 tests each checking one item you usually safe some work. You still have the same test coverage. So if your tests work, you are fine. If they fail, and you ignore it, you are also fine. In the case ...


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 ...


0

It is not a matter of programming language. If you'll write your library with 'this is general purpose library' in mind you can be successful. But, if I were your I'd start development of both in the same place, in the same repository. Later you can extract this library into another project.


0

looking up the LinkID at each insert seems inefficient You never tested it, I assume? Sounds like a typical case of premature optimization. Using proper indexing, especially for the URLs, should make a query for an URL roughly as fast as an INSERT into the same table (maybe the INSERT will become a little bit slower since the index has to be updated). ...


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, ...


-2

I'm a little confused. What is keeping you from just getting a portable copy of Python and running it off of a flash drive? I have a flash drive on my keys with a few portable compilers/IDEs and it all works fine. I suppose you could make a live USB if thought that you would often be switching between different operating systems, but I feel like in most ...


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: ...


0

valgrind with the option --tool=callgrind has the ability to capture dynamic calls. That together with kcachgrind or qcachegrind or grpof2dot will let you visualize the call graph.


0

The PEP 8 Python style guide shows lists in the form: my_list = [ 1, 2, 3, 4, 5, 6, ] In the indentation section of the pep https://www.python.org/dev/peps/pep-0008/#indentation For this reason I would say style 1 (ensuring not to violate the line length rules).


0

Personally, I'm using style2. Because its very easy for adding new item to list. Also with style2 you can easly delete item with deleting row. But its difficult on style1.


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 ...



Top 50 recent answers are included