Python is a dynamically typed, high-level interpreted programming language. Its design focuses on clear syntax, an intuitive approach to object-oriented programming, and making the right way to do things obvious. Python supports modules and exceptions, and has an extensive standard module library. ...
-5
votes
1answer
67 views
Detect if code is C
I am currently using Python to write a compiler manager. I have to detect whether given source code is C, even if the code has a few syntax errors. I am currently using the file's extension to tell, ...
0
votes
0answers
48 views
What should I consider while architecturing a loosely coupled modular application?
Introduction
For some background, I am trying to architecture an application for my startup. My application is divided in 3 separate git projects, details are as follows.
Backend API written using ...
3
votes
0answers
57 views
API design: stream objects vs. functions vs. messages
I'm designing API for a python library that accepts asynchronous input and produces asynchronous output: various signals come in, and various signals are generated in response (there's no one-to-one ...
0
votes
0answers
32 views
PSFL compatibility with Apache, LGPL, BSD, MIT
Description
I start with a simple real case example:
the foo.py, my secondary module:
import bar #third-party package that use a license (see under)
class Interface:
#do something (see below)
...
9
votes
4answers
102 views
When following SOLID, are reading and writing files two separate responsibilities?
I'm just starting to explore SOLID and I'm unsure if reading from files and writing to files are the same responsibility.
The target is the same file type; I want to read and write .pdf's in my ...
0
votes
0answers
36 views
Importing functions from modules in Python and JS
In Python importing just one function makes no performance benefit as pointed out in this question.
However, in JavaScript things are completely different, or at least I often see people do it and say ...
-1
votes
1answer
66 views
Classes for integrating both BitBucket and GitHub into our site (an inheritance and composition question)
I am writing a system of callbacks for BitBucket and GitHub which should modify our site on certain events in BitBucket or GitHub.
It is reasonable to make a base class like GitIntegration to handle ...
9
votes
3answers
468 views
Sometimes private functions are simply yet-to-be-extracted internal units of functionality. So why not test them?
Sometimes private functions of a module or class are simply yet-to-be-extracted internal units of functionality, which might deserve their own tests. So why not test them? We will write tests for them ...
0
votes
2answers
197 views
How many types of polymorphism are there in the Python language?
I just read an article by Luca Cardelli and he explained types of polymorphism which are:
The article is named On Understanding Types, Data Abstraction, and Polymorphism.
Types of Polymorphism
...
0
votes
2answers
88 views
What is the best/most Pythonic way to mock a private function?
Consider a module with a 'public' function which looks something like this:
def func(arg):
val = _generate_something(arg)
_do_something(val)
As you can see this is a 'void function'. Its ...
1
vote
4answers
155 views
Comparing objects with tolerance
The following code says that c1 == c2 and c2 == c3, but c1 != c3.
TOL = 0.11
class C:
def __init__(self, x):
self.x = x
def __eq__(self, other):
return abs(self.x - other.x) ...
18
votes
4answers
3k views
Functional style exception handling
I've been told that in functional programming one is not supposed to throw and/or observe exceptions. Instead an erroneous calculation should be evaluated as a bottom value. In Python (or other ...
3
votes
1answer
101 views
Why was the warning about % formatting toned down in newer Python docs?
The Python 3.1.5 docs refer to the % style formatting as "Old String Formatting Operations" and provide this warning that it may be removed:
Note: The formatting operations described here are ...
4
votes
3answers
165 views
How to DRY with calls to a database to execute?
I'm writing a python app that instructs a database to perform various processing instructions on partitioned tables. The processing can take long enough that there might be a timeout, so I surround my ...
-1
votes
0answers
16 views
reading and slicing a 2d list in python [migrated]
There is a file, just like this, called: test.txt:
John,19,7.5
Mary,22,9.8
Daniel,45,7.2
Hubert,92,10.0
Guy,28,9.5
I'm gonna extract the columns 2 to 4:
grades = np.genfromtxt(r'\test\test.txt',
...
8
votes
4answers
290 views
Class decorators in Python: practical use cases
I am looking for practical and non-synthetic use cases of Python class decorators. So far, the only case which made sense to me is registering a class in a publisher-subscriber system, e.g. plugins or ...
4
votes
2answers
154 views
How should I name functions that return values in Python?
I'm confused about choosing names for my functions in Python. Sometimes Python built-in functions are imperative such as: print function and string method find. Sometimes they aren't such as: len its ...
4
votes
3answers
262 views
Is it reasonable to write worse debug code in order to improve production code?
The title pretty much speaks for itself, but I'll provide the current decision I am facing.
I am migrating python code towards the use of generators. The current code looks like this:
...
l = ...
3
votes
1answer
116 views
Where do you put the “main function” of a Python app?
Suppose you are developing a Python standalone application (not a library).
Would you make it a Python package? Or just files next to each other?
If you would make it a package, what would be the "...
0
votes
1answer
92 views
Using singletons in Python (Django)
I was suggested to define a singleton in Python in the following way:
class Controller:
pass
# ...
controller = Controller()
However, this way, controller cannot be replaced with a derived ...
0
votes
1answer
59 views
Error checking design question (python & MVC)
Which is better as a design perspective?
An object that gets initialized and then checks to see if the parameters passed to it are valid before destroying itself (so in __init__) or
Check to see if ...
1
vote
1answer
56 views
suggestions on pygame code structuring?
so I made a simple game where boxes fall from the top of the screen and you have the dodge them from the bottom of the screen. When a box falls to the bottom of the screen without hitting you get a ...
0
votes
0answers
24 views
LALR parsing (with Ply) non-deterministic and sometimes fails
I have written a parser using Python Ply library.
While doing tests with an input file I have noticed a strange behaviour : sometimes the input is correctly parsed with no error and sometimes, there ...
0
votes
2answers
66 views
Using a class for a collection element, with methods to access other collections
I'm hoping for a sanity check in my design thinking.
I'm working with a small team on a website based on a MongoDB database. There are several collections in the DB -- for example, one representing ...
0
votes
0answers
23 views
Effective method for using both modified and original open-source Python module
I'm wondering if anyone has come up with an effective method to work on and use a modified version of an open-source Python module, while still using the unmodified one. (For example, to compare old ...
0
votes
1answer
34 views
Memory override using classes when using lists in python
I am trying to create a new variable with class Lamb (called hold) using a variable (main), which also has class Lamb. Lamb has two parameters (x and y).
I create a variable called main with class ...
5
votes
3answers
283 views
Does Python's defaultdict violate the LSP?
I believe, in Python defaultdict inherited from dict violates Liskov Substitution Principle. defaultdict doesn't raise KeyError while x in d is still False, for instance.
Is that so? If it is, why ...
2
votes
3answers
138 views
How to mock REST services for testing
I am currently building an application that consists of multiple small rest services that communicate between them. For example, a request to service A might make requests to services B and C in the ...
3
votes
4answers
186 views
Why raise an exception if python raises it for me?
I have a global position keeper of screen items so items don't need store their own positions.
class Screen_Position_Keeper:
# functions to add stuff
def get_px_row( self, item ):
...
0
votes
0answers
45 views
Two processes in a single docker container or two services connecting to the same db?
I recently started moving a monolithic application to microservices architecture using docker containers.
The general idea of the app is:
scraping data -> format the data -> save the data to MySQL -...
0
votes
0answers
39 views
How does the FOSS exception for MySQL connectors apply to code written in PHP or Python?
I have looked all over and can't find clear answers to the following:
If I write code in PHP or Python that uses GPL MySQL connectors for PHP or Python - which license applies to my code? That of PHP ...
0
votes
2answers
84 views
Creating instances of an ability when there are multiple different type of abilities
I'm creating an RPG game where a player has a set of skills, each of which he can level up to improve its effect. Here are two example skills:
health: increase player's maximum health
regeneration: ...
-2
votes
1answer
71 views
How to explain this method to find the least common multiple of the first n natural numbers?
This is the Project Euler question #5. The statement is pretty much finding the least common multiple of the first n natural numbers. So for example, the least common multiple of 1, 2 .. 10 is 2520.
...
-2
votes
1answer
66 views
How to ask a user how many inputs they want
I want to know if it possible to use arrays for a more viable alternative of getting a non-predetermined number of inputs as an array and with an indefinite number of inputs. (python)
original code --...
-1
votes
1answer
71 views
Can I use python 3.5.2 on my machine for development if the client is 3.4?
Here is the situation, I wish to use the last programming language of ruby, python and some more on my development machine, but the server where it will be is not last version of that programming ...
3
votes
2answers
152 views
How to decouple UI from logic on Pyqt/Qt apps properly?
I've read quite a lot about this subject in the past and watched some interesting talks like this one from Uncle Bob's. Still I always find pretty difficult to architect properly my desktop ...
3
votes
3answers
170 views
Stateful vs stateless (non web app) applications
I would like to understand the difference between stateful and stateless applications.
What would be a non web app example of a python application which is
stateless vs stateful?
Would a script which ...
-3
votes
2answers
54 views
Python function (or set of functions) as input of a function or class
is there a way of having an (yet unkown) function as an argument of a function in python:
def doSomething(<func>):
#do something with func()
at a later stage, functions could be defined, ...
2
votes
2answers
115 views
Function acting as a shortcut to object's methods
I was reading Python's requests library's code to find out how it works. Since this library has a simple usage interface, it creates a more complex object beyond. For instance:
requests.get(...)
Is ...
0
votes
1answer
252 views
Can I use Python with JavaScript as a UI?
First of all I am not a professional programmer, but a student with a programming hobby so I am sorry if my question is stupid.
I want to write a chess engine to be used on the web and had already ...
0
votes
0answers
15 views
How to protect quickly-incrementing variables in Django?
I want to count how many times an image is loaded on a webpage. We are looking at maybe 100 times per second.
One option is to store every impression as a row in a database, but this would get ...
1
vote
4answers
201 views
Why does string.find(“”) = 0? [closed]
I'm studying Python and hung up on a simple problem. Let's say we have:
string = "hello"
When we invoke the find method on the string to find an empty string like this:
string.find("")
Why does ...
-3
votes
1answer
96 views
Python recursion in functions [closed]
I am just learning about recursive functions. This was an example given in a textbook where t is a turtle. I was wondering how the code works, but more specifically, how the code runs past the first ...
0
votes
0answers
24 views
caveats while using one server as staging site for many projects?
EDIT: I realize this sounds like I am describing a development server, but I am actually hoping to test the project(s) on a node that is "as close to production" as possible. Contrast this with my ...
1
vote
3answers
135 views
Handle backward compatibility on API changes
I have an API which allows me to communication with a device.
The communication protocol is stored in a JSON file. It list the events that the device can raise, the functions, the frames format, etc.
...
0
votes
1answer
49 views
Can software under the “BSD 2-Clause License” be uploaded to PyPI?
Note: I'm not that experienced in working with licenses, so this could be a quite ignorant question. I've also read this other article on programmers.stackexchange before asking this question.
I've a ...
2
votes
3answers
175 views
Approach for refactoring a function that has lots of special braching
I am writing a function that operates slightly different depending on the data passed in. The common and unique code is mingled. I don't like what I have and am looking for a better way. Hopefully, ...
3
votes
1answer
215 views
Python-style Keyword Args in C++ - good practice or bad idea?
While trying to figure out the optimal ordering for optional parameters to a function recently, I stumbled across this blog post and accompanying GitHub repo, which provides a header for a Pythonic ...
1
vote
1answer
145 views
Can I really make python work with VBA
I'm a heavy VBA user that when has the chance to do a little project in python it's like going from hell to haven when it comes to programming itself. Still, I know that my users want reports in excel ...
2
votes
2answers
89 views
Setting class/methods conditionally
I want to keep the end-user (often myself) from being presented with inapplicable methods when coding directly in an interpreter, but I also don't want the user to have to inspect the incoming data ...